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

chore: tests fail on a machine with a ~/.cdk.json file #12579

Merged
merged 4 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ async function initCommandLine() {
debug('Command line arguments:', argv);

const configuration = new Configuration({
...argv,
_: argv._ as [Command, ...string[]], // TypeScript at its best
commandLineArguments: {
...argv,
_: argv._ as [Command, ...string[]], // TypeScript at its best
},
});
await configuration.load();

Expand Down
33 changes: 28 additions & 5 deletions packages/aws-cdk/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export type Arguments = {
readonly [name: string]: unknown;
};

export interface ConfigurationProps {
/**
* Configuration passed via command line arguments
*
* @default - Nothing passed
*/
readonly commandLineArguments?: Arguments;

/**
* Whether or not to use context from `.cdk.json` in user home directory
*
* @default true
*/
readonly readUserContext?: boolean;
}

/**
* All sources of settings combined
*/
Expand All @@ -66,9 +82,9 @@ export class Configuration {
private _projectContext?: Settings;
private loaded = false;

constructor(commandLineArguments?: Arguments) {
this.commandLineArguments = commandLineArguments
? Settings.fromCommandLineArguments(commandLineArguments)
constructor(private readonly props: ConfigurationProps = {}) {
this.commandLineArguments = props.commandLineArguments
? Settings.fromCommandLineArguments(props.commandLineArguments)
: new Settings();
this.commandLineContext = this.commandLineArguments.subSettings([CONTEXT_KEY]).makeReadOnly();
}
Expand All @@ -95,11 +111,18 @@ export class Configuration {
this._projectConfig = await loadAndLog(PROJECT_CONFIG);
this._projectContext = await loadAndLog(PROJECT_CONTEXT);

this.context = new Context(
const readUserContext = this.props.readUserContext ?? true;

const contextSources = [
this.commandLineContext,
this.projectConfig.subSettings([CONTEXT_KEY]).makeReadOnly(),
this.projectContext,
userConfig.subSettings([CONTEXT_KEY]).makeReadOnly());
];
if (readUserContext) {
contextSources.push(userConfig.subSettings([CONTEXT_KEY]).makeReadOnly());
}

this.context = new Context(...contextSources);

// Build settings from what's left
this.settings = this.defaultConfig
Expand Down
26 changes: 16 additions & 10 deletions packages/aws-cdk/test/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('load context from both files if available', async () => {
await fs.writeJSON('cdk.json', { context: { boo: 'far' } });

// WHEN
const config = await new Configuration().load();
const config = await new Configuration({ readUserContext: false }).load();

// THEN
expect(config.context.get('foo')).toBe('bar');
Expand All @@ -43,7 +43,7 @@ test('deleted context disappears from new file', async () => {
// GIVEN
await fs.writeJSON('cdk.context.json', { foo: 'bar' });
await fs.writeJSON('cdk.json', { context: { foo: 'bar' } });
const config = await new Configuration().load();
const config = await new Configuration({ readUserContext: false }).load();

// WHEN
config.context.unset('foo');
Expand All @@ -58,7 +58,7 @@ test('clear deletes from new file', async () => {
// GIVEN
await fs.writeJSON('cdk.context.json', { foo: 'bar' });
await fs.writeJSON('cdk.json', { context: { boo: 'far' } });
const config = await new Configuration().load();
const config = await new Configuration({ readUserContext: false }).load();

// WHEN
config.context.clear();
Expand All @@ -72,7 +72,7 @@ test('clear deletes from new file', async () => {
test('context is preserved in the location from which it is read', async () => {
// GIVEN
await fs.writeJSON('cdk.json', { context: { 'boo:boo': 'far' } });
const config = await new Configuration().load();
const config = await new Configuration({ readUserContext: false }).load();

// WHEN
expect(config.context.all).toEqual({ 'boo:boo': 'far' });
Expand All @@ -87,7 +87,7 @@ test('surive no context in old file', async () => {
// GIVEN
await fs.writeJSON('cdk.json', { });
await fs.writeJSON('cdk.context.json', { boo: 'far' });
const config = await new Configuration().load();
const config = await new Configuration({ readUserContext: false }).load();

// WHEN
expect(config.context.all).toEqual({ boo: 'far' });
Expand All @@ -100,35 +100,41 @@ test('surive no context in old file', async () => {
test('command line context is merged with stored context', async () => {
// GIVEN
await fs.writeJSON('cdk.context.json', { boo: 'far' });
const config = await new Configuration({ context: ['foo=bar'], _: ['command'] } as any).load();
const config = await new Configuration({
readUserContext: false,
commandLineArguments: {
context: ['foo=bar'],
_: ['command'],
} as any,
}).load();

// WHEN
expect(config.context.all).toEqual({ foo: 'bar', boo: 'far' });
});

test('can save and load', async () => {
// GIVEN
const config1 = await new Configuration().load();
const config1 = await new Configuration({ readUserContext: false }).load();
config1.context.set('some_key', 'some_value');
await config1.saveContext();
expect(config1.context.get('some_key')).toEqual('some_value');

// WHEN
const config2 = await new Configuration().load();
const config2 = await new Configuration({ readUserContext: false }).load();

// THEN
expect(config2.context.get('some_key')).toEqual('some_value');
});

test('transient values arent saved to disk', async () => {
// GIVEN
const config1 = await new Configuration().load();
const config1 = await new Configuration({ readUserContext: false }).load();
config1.context.set('some_key', { [TRANSIENT_CONTEXT_KEY]: true, value: 'some_value' });
await config1.saveContext();
expect(config1.context.get('some_key').value).toEqual('some_value');

// WHEN
const config2 = await new Configuration().load();
const config2 = await new Configuration({ readUserContext: false }).load();

// THEN
expect(config2.context.get('some_key')).toEqual(undefined);
Expand Down