Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #17

Merged
merged 23 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9c20c8d
test(index): add unit tests for index
ryansonshine May 13, 2021
f92ed93
fix(errors): fix invalid path to package.json
ryansonshine May 13, 2021
23edb6d
fix(types): remove namespace from context type
ryansonshine May 13, 2021
43fa87a
test(verify-npm): add unit tests for verify-npm
ryansonshine May 13, 2021
0ea6659
chore(npmrc): remove forgotten npmrc from root
ryansonshine May 14, 2021
74309d4
refactor(npmrc): add util for extracting registry from npmrc
ryansonshine May 14, 2021
a946552
chore(package): rearrange npm scripts
ryansonshine May 14, 2021
977aa24
test(verify-npm): add unit tests and additional use case handling
ryansonshine May 14, 2021
8e865a6
test(fixtures): fix typo on codeartifact urls
ryansonshine May 14, 2021
d66b780
test(mock-context): remove forgotten comments
ryansonshine May 14, 2021
ca990fd
chore(verify-npm): remove fogotten log statement
ryansonshine May 14, 2021
f0dd1d4
test(mock-context): update mock-context to return from function
ryansonshine May 14, 2021
10fb614
test(verify-config): add unit tests
ryansonshine May 14, 2021
15b6b41
refactor(errors): rename error code for aws_access_key_id
ryansonshine May 14, 2021
1c98da5
refactor(verify-npm): add fixtures for package.json
ryansonshine May 14, 2021
4176741
test(verify-auth): add unit tests
ryansonshine May 14, 2021
e6c79d6
fix(errors): fix homepage not resolving after bundle
ryansonshine May 14, 2021
ab06b26
refactor(types): remove SR_CA_NAMESPACE from process env types
ryansonshine May 16, 2021
6aa9e94
test(resolve-config): add unit tests
ryansonshine May 16, 2021
ab54c02
test(dummies): fix value in domainOwner to pass regex
ryansonshine May 16, 2021
ad65cfd
test(get-ca-config): add unit tests
ryansonshine May 16, 2021
4201dbb
refactor(errors): remove default params from plugin error
ryansonshine May 16, 2021
86a28fc
test(util): add additional test coverage
ryansonshine May 16, 2021
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
113 changes: 105 additions & 8 deletions package-lock.json

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

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
"license": "MIT",
"scripts": {
"build": "tsc",
"lint": "eslint ./src/ --fix",
"clean": "rm -rf ./lib/",
"test": "jest --coverage",
"start": "TS_NODE_FILES=true ts-node ./src/cli.ts",
"coverage": "codecov",
"cm": "cz",
"coverage": "codecov",
"lint": "eslint ./src/ --fix",
"semantic-release": "semantic-release",
"start": "TS_NODE_FILES=true ts-node ./src/cli.ts",
"test:watch": "jest --watch",
"semantic-release": "semantic-release"
"test": "jest --coverage",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@aws-sdk/client-codeartifact": "^3.13.1",
Expand All @@ -49,6 +50,7 @@
"@types/signale": "^1.4.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"aws-sdk-client-mock": "^0.4.0",
"codecov": "^3.8.1",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
Expand All @@ -62,6 +64,7 @@
"prettier": "^2.2.1",
"semantic-release": "^17.4.2",
"signale": "^1.4.0",
"tempy": "^1.0.1",
"ts-jest": "^26.5.5",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
Expand Down
28 changes: 25 additions & 3 deletions src/definitions/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import { ErrorDefinitions } from '../types';
import { SUPPORTED_TOOL_LIST } from './constants';

const [homepage]: string = require('../../../package.json').homepage.split('#');
const homepage =
'https://github.com/ryansonshine/semantic-release-codeartifact';

const linkify = (file: string): string => `${homepage}/blob/main/${file}`;

Expand All @@ -29,7 +30,7 @@ Your configuration for the \`tool\` option is \`${tool}\`.`,
'README.md#options'
)}) must be set in the plugin options.`,
}),
ENOAWSKEYID: () => ({
ENOAWSACCESSKEY: () => ({
message: 'No AWS access key specified.',
details: `An [AWS Access Key ID](https://docs.aws.amazon.com/general/latest/gr/glos-chap.html#accesskeyID) must be specified to get an auth token from CodeArtifact.

Expand All @@ -56,7 +57,7 @@ See [AWS Environment variables](${linkify(
'README.md#aws-environment-variables'
)}) for more details.`,
}),
EMISSINGPLUGIN: ({ plugin = '', tool = '', REQUIRED_PLUGINS = [] }) => ({
EMISSINGPLUGIN: ({ plugin, tool, REQUIRED_PLUGINS }) => ({
message: 'Missing plugin.',
details: `The plugin configuration is missing plugin '${plugin}' and is required for '${tool}'.

Expand All @@ -68,6 +69,20 @@ The required plugins for are: ['${REQUIRED_PLUGINS.join("','")}'].`,

The package.json \`publishConfig\` registry is '${publishConfig.registry}.'
The CodeArtifact endpoint is '${repositoryEndpoint}'.`,
}),
ENPMRCCONFIGMISMATCH: ({ repositoryEndpoint, registry }) => ({
message: 'Mismatch on CodeArtifact repository and npmrc registry',
details: `The registry set in the \`.npmrc\` of your project root does not match the CodeArtifact endpoint.

The \`.npmrc\` registry is '${registry}.'
The CodeArtifact endpoint is '${repositoryEndpoint}'.`,
}),
ENPMRCMULTIPLEREGISTRY: ({ registries }) => ({
message: 'Multiple registries found in npmrc',
details: `Your \`.npmrc\` contains multiple registries but should only contain one.

Please remove extraneous registries from your \`.npmrc\`.
Registries found: ['${registries.join("','")}'].`,
}),
ENOAUTHTOKEN: () => ({
message: 'No auth token returned from CodeArtifact client',
Expand All @@ -81,4 +96,11 @@ Please check your AWS configuration and try again.`,

Please check your AWS configuration and try again.`,
}),
EAWSSDK: ({ message, name }) => ({
message: 'AWS SDK Error',
details: `The AWS SDK threw an error while using the CodeArtifact client.

Name: '${name}'
Message: '${message}'`,
}),
};
21 changes: 17 additions & 4 deletions src/get-ca-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
} from '@aws-sdk/client-codeartifact';
import { getError } from './get-error';
import AggregateError from 'aggregate-error';
import { isAWSError } from './util/type-guards';

export const getCodeArtifactConfig = async (
pluginConfig: PluginConfig,
context: VerifyConditionsContext
): Promise<CodeArtifactConfig> => {
const errors = [];
const { env, logger } = context;
const { env } = context;
const { domain, domainOwner, tool, repository } = pluginConfig;
try {
const client = new CodeartifactClient({
Expand Down Expand Up @@ -54,8 +55,20 @@ export const getCodeArtifactConfig = async (

return { authorizationToken, repositoryEndpoint };
} catch (e) {
// TODO: handle aws sdk error
logger.error('Failed to get CodeArtifact credentials');
throw e;
if (e instanceof AggregateError) throw e;

if (isAWSError(e)) {
errors.push(getError('EAWSSDK', { message: e.message, name: e.name }));
} else {
errors.push(
getError('EAWSSDK', {
name: 'UnknownException',
message:
'An unknown error has occured, check the logs for more details.',
})
);
}

throw new AggregateError(errors);
}
};
7 changes: 5 additions & 2 deletions src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type SemanticReleaseError from '@semantic-release/error';
import type { WriteStream } from 'fs';
import type { Signale } from 'signale';
import type { KnownKeys } from './util';

/** Context keys shared across all lifecycle methods */
export interface CommonContext {
Expand Down Expand Up @@ -105,14 +106,16 @@ export interface PluginConfig extends OptionsBase {
domain: string;
/** The AWS Account ID that owns your CodeArtifact domain */
domainOwner?: string;
/** Associates a namespace with your repository tool */
namespace?: string;
/** The time, in seconds, that the login information is valid */
durationSeconds?: number;
/** Your CodeArtifact repository name */
repository: string;
}

export type PluginConfigKey = KnownKeys<
Exclude<PluginConfig, keyof OptionsBase>
>;

export type Channel = null | string;

export interface Release {
Expand Down
13 changes: 12 additions & 1 deletion src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ export interface ErrorDefinitions {
ENODOMAINSET: (...args: unknown[]) => ErrorDetails;
ENOREPOSET: () => ErrorDetails;
ENOAWSREGION: () => ErrorDetails;
ENOAWSKEYID: () => ErrorDetails;
ENOAWSACCESSKEY: () => ErrorDetails;
ENOAWSSECRETKEY: () => ErrorDetails;
EMISSINGPLUGIN: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
EPUBLISHCONFIGMISMATCH: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
ENPMRCCONFIGMISMATCH: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
ENPMRCMULTIPLEREGISTRY: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
ENOAUTHTOKEN: () => ErrorDetails;
ENOREPOENDPOINT: () => ErrorDetails;
EAWSSDK: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
}

export type AWSError = Error & {
$metadata: Record<string, unknown>;
code: string;
errno: string;
hostname: string;
syscall: string;
};
2 changes: 0 additions & 2 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ declare global {
SR_CA_DOMAIN?: string;
/** The AWS Account ID that owns your CodeArtifact domain */
SR_CA_DOMAIN_OWNER?: string;
/** Associates a namespace with your repository tool */
SR_CA_NAMESPACE?: string;
/** The time, in seconds, that the login information is valid */
SR_CA_DURATION_SEC?: string;
/** Your CodeArtifact repository name */
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './context';
export * from './errors';
export * from './code-artifact';
export * from './util';
6 changes: 6 additions & 0 deletions src/types/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// https://github.com/Microsoft/TypeScript/issues/25987#issuecomment-408339599
export type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
} extends { [_ in keyof T]: infer U }
? U
: never;
9 changes: 9 additions & 0 deletions src/util/npmrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const getRegistryFromNpmrc = (npmrc = ''): string[] => {
return npmrc
.split('\n')
.filter(config => config.includes('registry='))
.map(registry => {
const [, url] = registry.split('registry=');
return url;
});
};
4 changes: 4 additions & 0 deletions src/util/type-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AWSError } from '../types/errors';

export const isAWSError = (e: unknown): e is AWSError =>
Boolean(e && e instanceof Error && (e as AWSError).$metadata !== undefined);
Loading