Skip to content

Commit

Permalink
rename grep to test
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Sep 22, 2024
1 parent 2a70651 commit 56d8598
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/staxfile/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Expressions {
if (name === 'user_id') return process.getuid().toString()
if (name === 'dasherize') return dasherize(args[0])
if (name === 'exists') return existsSync(args[0]).toString()
if (name === 'grep') return this.grep(args[0], args[1]).toString()
if (name === 'test') return this.test(args[0], args[1]).toString()

this.staxfile.warnings.add(`Invalid template expression: ${name}`)
}
Expand Down Expand Up @@ -74,7 +74,7 @@ export default class Expressions {
'${{ stax.host_services }}:/run/host-services'
}

private grep(filename: string, pattern: string): boolean {
private test(filename: string, pattern: string): boolean {
const content = this.read(filename, '')
const regex = new RegExp(pattern)
return regex.test(content)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/staxfile/expressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Expressions', () => {
expect(staxfile.warnings).toContain("Undefined reference to 'stax.undefined_key'")
})

describe('grep', () => {
describe('test', () => {
let expressions
let mockStaxfile

Expand All @@ -96,19 +96,19 @@ describe('Expressions', () => {

it('returns true when pattern is found in file', () => {
mockStaxfile.location.readSync.mockImplementation(() => 'Hello, world!')
const result = expressions.evaluate('grep', ['test.txt', 'world'])
const result = expressions.evaluate('test', ['test.txt', 'world'])
expect(result).toBe('true')
})

it('returns false when pattern is not found in file', () => {
mockStaxfile.location.readSync.mockImplementation(() => 'Hello, world!')
const result = expressions.evaluate('grep', ['test.txt', 'foo'])
const result = expressions.evaluate('test', ['test.txt', 'foo'])
expect(result).toBe('false')
})

it('uses default value when file cannot be read', () => {
mockStaxfile.location.readSync.mockImplementation(() => { throw new Error('File not found') })
const result = expressions.evaluate('grep', ['nonexistent.txt', 'pattern'])
const result = expressions.evaluate('test', ['nonexistent.txt', 'pattern'])
expect(result).toBe('false')
})
})
Expand Down

0 comments on commit 56d8598

Please sign in to comment.