Skip to content
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
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Typical uses:
Provide the template as a file or direclty as text. If both are provided, the
file will be ignored.

| Input Name | Description | Required | Default |
| --------------- | ---------------------------------------------------------- | -------- | ------- |
| `template-file` | The path to a text file to load as the template. | No | - |
| `template-text` | The template text with variable placeholders. | No | - |
| `template-vars` | A JSON object containing variables to replace in the text. | Yes | - |
| Input Name | Description | Required | Default |
| --------------- | ------------------------------------------------ | -------- | ------- |
| `template-file` | The path to a text file to load as the template. | No | - |
| `template-text` | The template text with variable placeholders. | No | - |
| `template-vars` | An ENV style list or stringified JSON object. | Yes | - |

## Workflow Outputs

Expand All @@ -32,7 +32,7 @@ Action output. As such it can easily be referenced in subsequent steps.

## Scenarios

### Direct Text
### Direct Text (JSON variables)

```yaml
steps:
Expand All @@ -47,6 +47,22 @@ steps:
run: echo "${{ steps.build-comment.outputs.updated-text }}"
```

### Direct Text (ENV variables)

```yaml
steps:
- name: Build comment using template
id: build-comment
uses: chriswblake/action-text-variables@v1
with:
template-text: 'Hello {{ login }}, nice to meet you!'
template-vars: |
login=${{ github.actor }}

- name: Do something with result
run: echo "${{ steps.build-comment.outputs.updated-text }}"
```

### Use template from same repository

```yaml
Expand Down
228 changes: 184 additions & 44 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('action', () => {
jest.clearAllMocks()
})

// Proper Usage
it('Use template file', async () => {
// Proper Usage - JSON
it('Use template file - JSON', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(inputName => {
switch (inputName) {
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('action', () => {
expect(outputValue).not.toMatch(/extra value/)
})

it('Use template text', async () => {
it('Use template text - JSON', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(inputName => {
switch (inputName) {
Expand Down Expand Up @@ -95,6 +95,38 @@ describe('action', () => {
expect(outputValue).not.toMatch(/extra value/)
})

// Proper Usage - ENV format
it('Use template text - ENV', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(inputName => {
switch (inputName) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return `
name=John1
extra_var=extra value
`
default:
return ''
}
})

// Act - Load the template and replace the variables
await main.run()
expect(runMock).toHaveReturned()

// Assert - Check output name
const call = setOutputMock.mock.calls[0]
const outputName = call[0]
expect(outputName).toBe('updated-text')

// Assert - Check inserted values
const outputValue = call[1]
expect(outputValue).toBe('Hello John1')
expect(outputValue).not.toMatch(/extra value/)
})

// Error Responses - Missing inputs
it('Missing template. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
Expand Down Expand Up @@ -163,53 +195,161 @@ describe('action', () => {
expect.stringMatching(/Missing required input/)
)
})
})

// Error Responses - Bad Inputs
it('Provided non-JSON for template-vars. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(name => {
switch (name) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return 1234
default:
return undefined
}
// Error Responses - Bad Inputs
it('Provided number for template-vars. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(name => {
switch (name) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return 1234
default:
return undefined
}
})

// Act - Run action to cause the error
await main.run()
expect(runMock).toHaveReturned()

// Assert - Action was closed with correct error message
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
expect.stringMatching(/Invalid input/)
)
})

// Act - Run action to cause the error
await main.run()
expect(runMock).toHaveReturned()
it('Provided empty string for template-vars. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(name => {
switch (name) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return ''
default:
return undefined
}
})

// Assert - Action was closed with correct error message
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
expect.stringMatching(/Invalid JSON input/)
)
})
// Act - Run action to cause the error
await main.run()
expect(runMock).toHaveReturned()

// Assert - Action was closed with correct error message
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
expect.stringMatching(/Missing required input/)
)
})

it('Badly formed JSON for template-vars. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(name => {
switch (name) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return '{ forgot quotations on values }'
default:
return ''
}
})

// Act - Run action to cause the error
await main.run()
expect(runMock).toHaveReturned()

// Assert - Action was closed with correct error message
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
expect.stringMatching(/Invalid input/)
)
})

it('Badly formed JSON for template-vars. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(name => {
switch (name) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return '{ forgot quotations on values }'
default:
return ''
}
it('Badly formed variable list for template-vars. Set failed status.', async () => {
// Arrange - Mock responses for the inputs
getInputMock.mockImplementation(name => {
switch (name) {
case 'template-text':
return 'Hello {{ name }}'
case 'template-vars':
return `
name!=John1
`
default:
return ''
}
})

// Act - Run action to cause the error
await main.run()
expect(runMock).toHaveReturned()

// Assert - Action was closed with correct error message
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
expect.stringMatching(/Invalid input/)
)
})

// Methods
it('parseTemplateVars - JSON', () => {
// Arrange
const variables = JSON.stringify({
name: 'John1',
person: {
name: 'John2',
unused_value: 'unused'
},
multiline_paragraph: `
Line 1
Line 2
`,
extra_var: 'extra value'
})
// Act
const result = main.parseTemplateVars(variables)

// Assert
expect(result.name).toEqual('John1')
expect(result['person']['name']).toEqual('John2')
expect(result['person']['unused_value']).toEqual('unused')
expect(result.multiline_paragraph).toMatch(/Line 1\s*Line 2/)
})

// Act - Run action to cause the error
await main.run()
expect(runMock).toHaveReturned()
it('parseTemplateVars - ENV', () => {
// Arrange
const variables = `
name=John1
person.name=John2
person.unused_value=unused
multiline_paragraph="
Line 1
Line 2
"
`
// Act
const result = main.parseTemplateVars(variables)

// Assert - Action was closed with correct error message
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
expect.stringMatching(/Invalid JSON input/)
)
// Assert
expect(result.name).toEqual('John1')
expect(result['person.name']).toEqual('John2')
expect(result['person.unused_value']).toEqual('unused')
expect(result.multiline_paragraph).toMatch(/Line 1\s*Line 2/)
})

it('parseTemplateVars - invalid ENV', () => {
// Arrange
const variables = `
name!=John1
`
// Act
const parseAction = () => main.parseTemplateVars(variables)

// Assert
expect(parseAction).toThrow(/Invalid input/)
})
})
Loading
Loading