Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for escaping brackets used in paths #1115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

46 changes: 45 additions & 1 deletion src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path'
import {getDeletedFiles, normalizeSeparators} from '../utils'
import {escapeString, getDeletedFiles, normalizeSeparators} from '../utils'

const {GITHUB_WORKSPACE} = process.env
const topLevelDir = `${GITHUB_WORKSPACE}${path.sep}`
Expand Down Expand Up @@ -183,4 +183,48 @@ describe('utils test', () => {
expect(deletedFiles).toContain(path.join(topLevelDir, 'entrypoint.sh'))
})
})

describe('escapeString', () => {
// Returns the input string if it contains no special characters
it('should return the input string when it contains no special characters', () => {
const input = 'hello world'
const result = escapeString(input)
expect(result).toBe(input)
})

// Escapes special characters for bash shell if they exist in the input string
it('should escape special characters for bash shell if they exist in the input string', () => {
const input = 'hello $world'
const result = escapeString(input)
expect(result).toBe('hello \\$world')
})

// Escapes square brackets in the input string
it('should escape square brackets in the input string', () => {
const input = 'hello [world]'
const result = escapeString(input)
expect(result).toBe('hello \\[world\\]')
})

// Returns an empty string if the input string is empty
it('should return an empty string when the input string is empty', () => {
const input = ''
const result = escapeString(input)
expect(result).toBe('')
})

// Escapes all special characters if they all exist in the input string
it('should escape all special characters if they all exist in the input string', () => {
const input = ':*?<>|;`$()&!'
const result = escapeString(input)
expect(result).toBe('\\:\\*\\?\\<\\>\\|\\;\\`\\$\\(\\)\\&\\!')
})

// Escapes special characters at the beginning and end of the input string
it('should escape special characters at the beginning and end of the input string', () => {
const input = '!hello!'
const result = escapeString(input)
expect(result).toBe('\\!hello\\!')
})
})
})
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,15 @@ export async function tempfile(extension = ''): Promise<string> {
return path.join(tempDirectory, `${uuidv4()}${extension}`)
}

/**
* Escapes special characters in a string for the bash shell.
*
* @param value - The string to be escaped.
* @returns The escaped string.
*/
export function escapeString(value: string): string {
// escape special characters for bash shell
return value.replace(/[^\x20-\x7E]|[:*?<>|;`$()&!]/g, '\\$&')
return value.replace(/[^\x20-\x7E]|[:*?<>|;`$()&!]|\[|]/g, '\\$&')
}

export async function exists(filePath: string): Promise<boolean> {
Expand Down
Loading