Skip to content

Commit

Permalink
feat: add support for escaping brackets used in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Feb 28, 2024
1 parent 383119c commit 91d4ecc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
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

0 comments on commit 91d4ecc

Please sign in to comment.