Skip to content

Commit 1cb8471

Browse files
feat!: replace env style variables for yaml style variables (#22)
* feat: replace env style variables for yaml style variables * feat: update tests to use YAML format for template variables * test: refactor action tests to use JSON and YAML variable formats in separate jobs * test: update action tests to include objects for template-text test cases * linting * Cleanup
1 parent 7d66673 commit 1cb8471

File tree

8 files changed

+4176
-367
lines changed

8 files changed

+4176
-367
lines changed

.github/workflows/ci.yml

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,68 +41,95 @@ jobs:
4141
id: npm-ci-test
4242
run: npm run ci-test
4343

44-
run-action-tests:
45-
name: Test Action
44+
run-action-tests-json:
45+
name: Test Action with JSON vars
4646
runs-on: ubuntu-latest
4747
steps:
4848
- name: Checkout
4949
uses: actions/checkout@v4
5050

51-
- name: Direct Text (JSON Variables)
52-
id: direct-json
51+
- name: Template Text
52+
id: template-text
5353
uses: ./
5454
with:
55-
template-text: 'Hello {{ login }}, nice to meet you!'
56-
template-vars: >
55+
template-text:
56+
'Hello {{ login }}, nice to meet you {{ person.role }}!'
57+
template-vars: |
5758
{
58-
"login": "Octobi Wan Catnobi"
59+
"login": "Octobi Wan Catnobi",
60+
"person": { "role": "Master Jedi" }
5961
}
6062
61-
- name: Verify Direct Text JSON Output
63+
- name: Verify Template Text Output
6264
env:
63-
UPDATED_TEXT: ${{ steps.direct-json.outputs.updated-text }}
65+
UPDATED_TEXT: ${{ steps.template-text.outputs.updated-text }}
6466
run: |
6567
echo "$UPDATED_TEXT" | grep -q "Octobi Wan Catnobi" || exit 1
68+
echo "$UPDATED_TEXT" | grep -q "Master Jedi" || exit 1
6669
67-
- name: Direct Text (ENV Variables)
68-
id: direct-env
70+
- name: Template File
71+
id: template-file
6972
uses: ./
7073
with:
71-
template-text: 'Hello {{ login }}, nice to meet you!'
74+
template-file: ./__tests__/sample-template.md
7275
template-vars: |
73-
login=Octoclark Kentocat
76+
{
77+
"name": "Spidertocat",
78+
"person": { "name": "Spider Person" },
79+
"multiline_paragraph": "This is a multiline\nparagraph example."
80+
}
7481
75-
- name: Verify Direct Text ENV Output
82+
- name: Verify Text File JSON Output
7683
env:
77-
UPDATED_TEXT: ${{ steps.direct-env.outputs.updated-text }}
84+
UPDATED_TEXT: ${{ steps.template-file.outputs.updated-text }}
7885
run: |
79-
echo "$UPDATED_TEXT" | grep -q "Octoclark Kentocat" || exit 1
86+
echo "$UPDATED_TEXT" | grep -q "Spidertocat" || exit 1
87+
echo "$UPDATED_TEXT" | grep -q "Spider Person" || exit 1
88+
echo "$UPDATED_TEXT" | grep -q "This is a multiline" || exit 1
89+
echo "$UPDATED_TEXT" | grep -q "paragraph example." || exit 1
8090
81-
- name: Text File (JSON variables)
82-
id: template-file-json
91+
run-action-tests-yaml:
92+
name: Test Action with YAML vars
93+
runs-on: ubuntu-latest
94+
steps:
95+
- name: Checkout
96+
uses: actions/checkout@v4
97+
98+
- name: Template Text
99+
id: template-text
83100
uses: ./
84101
with:
85-
template-file: ./examples/hello.md
86-
template-vars: >
87-
{
88-
"login": "Spidertocat"
89-
}
102+
template-text:
103+
'Hello {{ login }}, nice to meet you {{ person.role }}!'
104+
template-vars: |
105+
login: Octoclark Kentocat
106+
person:
107+
role: Librarian
90108
91-
- name: Verify Text File JSON Output
109+
- name: Verify Template Text Output
92110
env:
93-
UPDATED_TEXT: ${{ steps.template-file-json.outputs.updated-text }}
111+
UPDATED_TEXT: ${{ steps.template-text.outputs.updated-text }}
94112
run: |
95-
echo "$UPDATED_TEXT" | grep -q "Spidertocat" || exit 1
113+
echo "$UPDATED_TEXT" | grep -q "Octoclark Kentocat" || exit 1
114+
echo "$UPDATED_TEXT" | grep -q "Librarian" || exit 1
96115
97-
- name: Text File (ENV variables)
98-
id: template-file-env
116+
- name: Template File
117+
id: template-file
99118
uses: ./
100119
with:
101-
template-file: ./examples/hello.md
102-
template-vars: login=Codercat
103-
104-
- name: Verify Text File ENV Output
120+
template-file: ./__tests__/sample-template.md
121+
template-vars: |
122+
name: Codercat
123+
person:
124+
name: Coder Person
125+
multiline_paragraph: |
126+
This is a yaml multiline
127+
paragraph example.
128+
129+
- name: Verify Template File Output
105130
env:
106-
UPDATED_TEXT: ${{ steps.template-file-env.outputs.updated-text }}
131+
UPDATED_TEXT: ${{ steps.template-file.outputs.updated-text }}
107132
run: |
108133
echo "$UPDATED_TEXT" | grep -q "Codercat" || exit 1
134+
echo "$UPDATED_TEXT" | grep -q "This is a yaml multiline" || exit 1
135+
echo "$UPDATED_TEXT" | grep -q "paragraph example." || exit 1

__tests__/main.test.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ describe('action', () => {
9393
expect(outputValue).not.toMatch(/extra value/)
9494
})
9595

96-
// Proper Usage - ENV format
97-
it('Use template text - ENV', async () => {
96+
// Proper Usage - YAML format
97+
it('Use template text - YAML', async () => {
9898
// Arrange - Mock responses for the inputs
9999
getInputMock.mockImplementation(inputName => {
100100
switch (inputName) {
101101
case 'template-text':
102102
return 'Hello {{ name }}'
103103
case 'template-vars':
104104
return `
105-
name=John1
106-
extra_var=extra value
105+
name: John1
106+
extra_var: extra value
107107
`
108108
default:
109109
return ''
@@ -250,7 +250,34 @@ describe('action', () => {
250250
case 'template-text':
251251
return 'Hello {{ name }}'
252252
case 'template-vars':
253-
return '{ forgot quotations on values }'
253+
return '{ "name": "John, "forgot": "closing quote" }'
254+
default:
255+
return ''
256+
}
257+
})
258+
259+
// Act - Run action to cause the error
260+
await main.run()
261+
expect(runMock).toHaveReturned()
262+
263+
// Assert - Action was closed with correct error message
264+
expect(setFailedMock).toHaveBeenNthCalledWith(
265+
1,
266+
expect.stringMatching(/Invalid input/)
267+
)
268+
})
269+
270+
it('Badly formed YAML for template-vars. Set failed status.', async () => {
271+
// Arrange - Mock responses for the inputs
272+
getInputMock.mockImplementation(name => {
273+
switch (name) {
274+
case 'template-text':
275+
return 'Hello {{ name }}'
276+
case 'template-vars':
277+
return `
278+
name: "John1
279+
invalid: yaml: format
280+
`
254281
default:
255282
return ''
256283
}
@@ -303,8 +330,8 @@ describe('action', () => {
303330
unused_value: 'unused'
304331
},
305332
multiline_paragraph: `
306-
Line 1
307-
Line 2
333+
Line 1
334+
Line 2
308335
`,
309336
extra_var: 'extra value'
310337
})
@@ -318,31 +345,32 @@ describe('action', () => {
318345
expect(result.multiline_paragraph).toMatch(/Line 1\s*Line 2/)
319346
})
320347

321-
it('parseTemplateVars - ENV', () => {
348+
it('parseTemplateVars - YAML', () => {
322349
// Arrange
323350
const variables = `
324-
name=John1
325-
person.name=John2
326-
person.unused_value=unused
327-
multiline_paragraph="
351+
name: John1
352+
person:
353+
name: John2
354+
unused_value: unused
355+
multiline_paragraph: |
328356
Line 1
329357
Line 2
330-
"
331358
`
332359
// Act
333360
const result = main.parseTemplateVars(variables)
334361

335362
// Assert
336363
expect(result.name).toEqual('John1')
337-
expect(result['person.name']).toEqual('John2')
338-
expect(result['person.unused_value']).toEqual('unused')
364+
expect(result.person.name).toEqual('John2')
365+
expect(result.person.unused_value).toEqual('unused')
339366
expect(result.multiline_paragraph).toMatch(/Line 1\s*Line 2/)
340367
})
341368

342-
it('parseTemplateVars - invalid ENV', () => {
369+
it('parseTemplateVars - invalid YAML', () => {
343370
// Arrange
344371
const variables = `
345-
name!=John1
372+
name: "John1
373+
invalid: yaml: format
346374
`
347375
// Act
348376
const parseAction = () => main.parseTemplateVars(variables)

0 commit comments

Comments
 (0)