Skip to content

Commit

Permalink
Implement for nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrbradley committed Jun 17, 2024
1 parent d0bb699 commit af4c706
Show file tree
Hide file tree
Showing 20 changed files with 99,458 additions and 829 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ jobs:
id: checkout
uses: actions/checkout@v4

- name: Test Local Action
id: test-action
- name: Test Local Action for nodejs
uses: ./
with:
milliseconds: 2000

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.time }}"
language: nodejs
directory: __tests__/programs/random-nodejs
provider: random
providerVersion: '4.16.2'
128 changes: 108 additions & 20 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import * as main from '../src/main'
// Mock the action's main function
const runMock = jest.spyOn(main, 'run')

// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/

// Mock the GitHub Actions core library
let debugMock: jest.SpiedFunction<typeof core.debug>
let errorMock: jest.SpiedFunction<typeof core.error>
Expand All @@ -23,6 +20,9 @@ let setFailedMock: jest.SpiedFunction<typeof core.setFailed>
let setOutputMock: jest.SpiedFunction<typeof core.setOutput>

describe('action', () => {
// Increase the Jest timeout to 30 seconds as we're download dependencies
jest.setTimeout(30 * 1000)

beforeEach(() => {
jest.clearAllMocks()

Expand All @@ -33,12 +33,40 @@ describe('action', () => {
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
})

it('sets the time output', async () => {
it('valid setup', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'language':
return 'nodejs'
case 'directory':
return '__tests__/programs/random-nodejs'
case 'provider':
return 'random'
case 'providerVersion':
return '4.16.2'
case 'publisher':
return 'pulumi'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

expect(setFailedMock).not.toHaveBeenCalled()
expect(errorMock).not.toHaveBeenCalled()
expect(debugMock).toHaveBeenCalled()
expect(setOutputMock).toHaveBeenCalledTimes(0)
})

it('invalid language', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500'
case 'language':
return 'this is not a language'
default:
return ''
}
Expand All @@ -48,29 +76,89 @@ describe('action', () => {
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Unsupported language: this is not a language'
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
expect(errorMock).not.toHaveBeenCalled()
})

it('directory does not exist', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'language':
return 'nodejs'
case 'directory':
return '__tests__/programs/nonexistent'
case 'provider':
return 'random'
case 'providerVersion':
return '4.16.2'
case 'publisher':
return 'pulumi'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
"Can't access directory __tests__/programs/nonexistent: Error: ENOENT: no such file or directory, access '__tests__/programs/nonexistent'"
)
expect(setOutputMock).toHaveBeenNthCalledWith(
expect(errorMock).not.toHaveBeenCalled()
})

it('invalid provider version', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'language':
return 'nodejs'
case 'directory':
return '__tests__/programs/random-nodejs'
case 'provider':
return 'random'
case 'providerVersion':
return 'not a version'
case 'publisher':
return 'pulumi'
default:
return ''
}
})

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
'Invalid provider version: not a version'
)
expect(errorMock).not.toHaveBeenCalled()
})

it('sets a failed status', async () => {
it('invalid package version', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
case 'language':
return 'nodejs'
case 'directory':
return '__tests__/programs/random-nodejs'
case 'provider':
return 'random'
case 'providerVersion':
return '4.16.2'
case 'packageVersion':
return 'not a version'
case 'publisher':
return 'pulumi'
default:
return ''
}
Expand All @@ -82,7 +170,7 @@ describe('action', () => {
// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
'Invalid package version: not a version'
)
expect(errorMock).not.toHaveBeenCalled()
})
Expand Down
3 changes: 3 additions & 0 deletions __tests__/programs/random-nodejs/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: simple-random
runtime: nodejs
description: A simple example of using various random resources.
3 changes: 3 additions & 0 deletions __tests__/programs/random-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# examples/simple

A simple example of using various random APIs.
47 changes: 47 additions & 0 deletions __tests__/programs/random-nodejs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as random from '@pulumi/random'

Check failure on line 15 in __tests__/programs/random-nodejs/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unable to resolve path to module '@pulumi/random'

export const randomId = new random.RandomId('id', { byteLength: 4 }).hex
export const randomShuffle = new random.RandomShuffle('shuffle', {
inputs: ['a', 'b', 'c']
}).results
export const randomString = new random.RandomString('string', { length: 32 })
.result
export const randomInteger = new random.RandomInteger('integer', {
min: 128,
max: 1024
}).result
export const randomUuid = new random.RandomUuid('uuid').result
export const randomPassword = new random.RandomPassword('password', {
length: 32
}).result
export const randomPet = new random.RandomPet('pet').id

export const randomPasswordWithKeepers = new random.RandomPassword(
'passwordWithKeepers',
{
length: 32,
keepers: { pwdseed1: 'pwdseed1' }
}
).result

export const randomStringWithWarning = new random.RandomString(
'stringWithWarning',
{
length: 32,
number: false // this is deprecated, but Pulumi program should not fail
}
).result
15 changes: 15 additions & 0 deletions __tests__/programs/random-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "simple",
"version": "0.0.1",
"license": "Apache-2.0",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/random": "^4.0.0"
},
"devDependencies": {
"@types/node": "^8.0.0"
}
}
16 changes: 16 additions & 0 deletions __tests__/programs/random-nodejs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.ts"]
}
25 changes: 0 additions & 25 deletions __tests__/wait.test.ts

This file was deleted.

10 changes: 7 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ inputs:
directory:
description: Path to a Pulumi program to use for the test.
required: true
version:
description: The version of the provider to be tested.
required: true
provider:
description: The name of the provider (excluding the publisher prefix)
required: true
providerVersion:
description: The version of the provider to be tested.
required: true
packageVersion:
description:
The version of the package to be tested. Defaults to ${{ providerVersion
}} if not set.
publisher:
description: The publisher of the provider (if not pulumi)
default: pulumi
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions dist/exec-child.js

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

Loading

0 comments on commit af4c706

Please sign in to comment.