1
0
Fork 0
mirror of synced 2026-06-05 19:18:19 +00:00

chore: add initial tests

This commit is contained in:
Tom Keller 2022-10-15 20:25:56 -07:00
commit a61ce85bf3
No known key found for this signature in database
GPG key ID: E1806C1EE1663B8D
8 changed files with 944 additions and 556 deletions

2
.projen/tasks.json generated
View file

@ -87,7 +87,7 @@
"description": "Synthesize project files",
"steps": [
{
"exec": "node .projenrc.cjs"
"exec": "node .projenrc.js"
}
]
},

View file

@ -5,7 +5,12 @@ const { NodePackageManager, NpmAccess } = require('projen/lib/javascript');
const project = new GitHubActionTypeScriptProject({
defaultReleaseBranch: 'main',
devDeps: ['projen-github-action-typescript', '@aws-sdk/credential-provider-env', 'aws-sdk-client-mock', '@jest/globals'],
devDeps: [
'projen-github-action-typescript',
'@aws-sdk/credential-provider-env',
'aws-sdk-client-mock',
'@jest/globals',
],
deps: ['@aws-sdk/client-sts@^3'],
name: 'configure-aws-credentials',
description: 'A GitHub Action to configure AWS credentials',
@ -15,9 +20,6 @@ const project = new GitHubActionTypeScriptProject({
authorOrganization: true,
authorUrl: 'https://aws.amazon.com',
packageManager: NodePackageManager.NPM,
projenrcJsOptions: {
filename: '.projenrc.cjs',
},
sampleCode: false,
actionMetadata: {
name: '"Configure AWS Credentials" Action for GitHub Actions',
@ -130,7 +132,8 @@ const project = new GitHubActionTypeScriptProject({
inlineSourceMap: true,
strict: true,
// Node 16 is ES2022
target: 'ES2022',
target: 'es2022',
module: 'commonjs',
outDir: 'build',
},
},
@ -145,10 +148,14 @@ const project = new GitHubActionTypeScriptProject({
bracketSpacing: true,
},
},
jestOptions: {
jestConfig: {
transform: { '^.+\\.m?[tj]sx?$': ['ts-jest', { tsconfig: 'tsconfig.dev.json' }] },
},
},
dependabot: true,
dependabotOptions: {
scheduleInterval: DependabotScheduleInterval.WEEKLY,
},
githubOptions: {
mergify: true,
@ -212,21 +219,12 @@ if (tsconfig) {
}
// The default jest config does not have the correct path
project.jest?.addTestMatch('<rootDir>/test/**/*.(test|spec).(js|jsx|ts|tsx)');
// Future-proofing in case we decide to move to ESM
project.setScript('projen', 'node .projenrc.cjs');
// Most jest overrides. Specifically, work around the deprecation of the jest globals config
const packageJson = project.tryFindFile('package.json');
if (packageJson) {
packageJson.addOverride('jest.transform', {
'^.+\\.[tj]sx?$': [
'ts-jest',
{
useESM: true,
tsconfig: 'tsconfig.dev.json',
},
],
});
// The default jest config makes use of the deprecated globals.ts-jest option
packageJson.addOverride('jest.globals', undefined);
// This is supposed to be controlled by jestConfig.preset, but it doesn't work
packageJson.addOverride('jest.preset', 'ts-jest/presets/default-legacy');
// The entrypoint property is supposed to manage this but it doesn't work
packageJson.addOverride('main', 'build/index.js');
// We don't want to publish this to NPM.
@ -243,7 +241,7 @@ if (dependabotConfig) {
directory: '/',
'open-pull-requests-limit': 10,
'target-branch': 'v1-node16',
ignore: [ { 'dependency-name': 'projen' } ],
ignore: [{ 'dependency-name': 'projen' }],
'versioning-strategy': 'lockfile-only',
schedule: { interval: 'weekly', day: 'tuesday' },
});

1328
package-lock.json generated

File diff suppressed because it is too large Load diff

22
package.json generated
View file

@ -17,7 +17,7 @@
"test:watch": "npx projen test:watch",
"unbump": "npx projen unbump",
"watch": "npx projen watch",
"projen": "node .projenrc.cjs"
"projen": "npx projen"
},
"author": {
"name": "Amazon.com, Inc. or its affiliates",
@ -26,6 +26,7 @@
},
"devDependencies": {
"@aws-sdk/credential-provider-env": "^3.186.0",
"@aws-sdk/credential-providers": "^3.188.0",
"@jest/globals": "^29.1.2",
"@types/jest": "^29.1.2",
"@types/node": "^14",
@ -70,6 +71,14 @@
"url": "https://github.com/aws-actions/configure-aws-credentials/issues"
},
"jest": {
"transform": {
"^.+\\.m?[tj]sx?$": [
"ts-jest",
{
"tsconfig": "tsconfig.dev.json"
}
]
},
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.ts?(x)",
"<rootDir>/(test|src)/**/*(*.)@(spec|test).ts?(x)",
@ -103,16 +112,7 @@
}
]
],
"preset": "ts-jest",
"transform": {
"^.+\\.[tj]sx?$": [
"ts-jest",
{
"useESM": true,
"tsconfig": "tsconfig.dev.json"
}
]
}
"preset": "ts-jest/presets/default-legacy"
},
"types": "build/index.d.ts",
"overrides": {

View file

@ -38,9 +38,17 @@ export function isDefined<T>(i: T | undefined | null): i is T {
return i !== undefined && i !== null;
}
function sleep(ms: number) {
function defaultSleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
let sleep = defaultSleep;
export function withsleep(s: typeof sleep) {
sleep = s;
}
export function reset() {
sleep = defaultSleep;
}
// retryAndBackoff retries with exponential backoff the promise if the error isRetryable upto maxRetries time.
export async function retryAndBackoff<T>(

52
test/cleanup.test.ts Normal file
View file

@ -0,0 +1,52 @@
import * as core from '@actions/core';
import { cleanup } from '../src/cleanup';
const FAKE_ACCESS_KEY_ID = 'MY-AWS-ACCESS-KEY-ID';
const FAKE_SECRET_ACCESS_KEY = 'MY-AWS-SECRET-ACCESS-KEY';
const FAKE_SESSION_TOKEN = 'MY-AWS-SESSION-TOKEN';
const FAKE_REGION = 'fake-region-1';
const ACTION_ENVIRONMENT_VARIABLES = {
AWS_ACCESS_KEY_ID: FAKE_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: FAKE_SECRET_ACCESS_KEY,
AWS_SESSION_TOKEN: FAKE_SESSION_TOKEN,
AWS_DEFAULT_REGION: FAKE_REGION,
AWS_REGION: FAKE_REGION,
};
describe('Configure AWS Credentials', () => {
const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules();
jest.spyOn(core, 'exportVariable');
jest.spyOn(core, 'setSecret');
jest.spyOn(core, 'setOutput');
jest.spyOn(core, 'setFailed');
process.env = { ...OLD_ENV, ...ACTION_ENVIRONMENT_VARIABLES };
});
afterEach(() => {
process.env = OLD_ENV;
});
test('replaces AWS credential and region env vars with empty strings', async () => {
await cleanup();
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(core.exportVariable).toHaveBeenCalledTimes(5);
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', '');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', '');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SESSION_TOKEN', '');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', '');
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', '');
});
test('error is caught and fails the action', async () => {
jest.spyOn(core, 'exportVariable').mockImplementation(() => {
throw new Error();
});
await cleanup();
expect(core.setFailed).toHaveBeenCalled();
});
});

4
tsconfig.dev.json generated
View file

@ -9,7 +9,7 @@
"lib": [
"es2019"
],
"module": "CommonJS",
"module": "commonjs",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
@ -22,7 +22,7 @@
"strictNullChecks": true,
"strictPropertyInitialization": true,
"stripInternal": true,
"target": "ES2022",
"target": "es2022",
"forceConsistentCasingInFileNames": true,
"outDir": "build"
},

4
tsconfig.json generated
View file

@ -11,7 +11,7 @@
"lib": [
"es2019"
],
"module": "CommonJS",
"module": "commonjs",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
@ -24,7 +24,7 @@
"strictNullChecks": true,
"strictPropertyInitialization": true,
"stripInternal": true,
"target": "ES2022",
"target": "es2022",
"forceConsistentCasingInFileNames": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,