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

feat(remodel): make tests more directory agnostic #24359

Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions packages/@aws-cdk/aws-ecr-assets/test/tarball-asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import { TarballImageAsset } from '../lib';


describe('image asset', () => {
const tarballFile = path.join(__dirname, 'demo-tarball', 'empty.tar');
test('test instantiating Asset Image', () => {
// GIVEN
const app = new App();
const stack = new Stack(app);
const asset = new TarballImageAsset(stack, 'Image', {
tarballFile: __dirname + '/demo-tarball/empty.tar',
tarballFile,
});

// WHEN
Expand Down Expand Up @@ -56,7 +57,7 @@ describe('image asset', () => {
const stack = new Stack();
const user = new iam.User(stack, 'MyUser');
const asset = new TarballImageAsset(stack, 'Image', {
tarballFile: 'test/demo-tarball/empty.tar',
tarballFile,
});

// WHEN
Expand Down Expand Up @@ -118,7 +119,7 @@ describe('image asset', () => {
const app = new App();
const stack = new Stack(app);
const image = new TarballImageAsset(stack, 'MyAsset', {
tarballFile: 'test/demo-tarball/empty.tar',
tarballFile,
});

const session = app.synth();
Expand All @@ -145,10 +146,10 @@ describe('image asset', () => {
}),
});
const asset1 = new TarballImageAsset(stack1, 'MyAsset', {
tarballFile: 'test/demo-tarball/empty.tar',
tarballFile,
});
const asset2 = new TarballImageAsset(stack2, 'MyAsset', {
tarballFile: 'test/demo-tarball/empty.tar',
tarballFile,
});

test('stack with default synthesizer', () => {
Expand Down
14 changes: 13 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Architecture, Code, Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
Expand Down Expand Up @@ -641,7 +642,7 @@ test('esbuild bundling with pre compilations', () => {
architecture: Architecture.X86_64,
});

const compilerOptions = util.getTsconfigCompilerOptions(path.join(__dirname, '..', 'tsconfig.json'));
const compilerOptions = util.getTsconfigCompilerOptions(findParentTsConfigPath(__dirname));

// Correctly bundles with esbuild
expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(packageLock), {
Expand Down Expand Up @@ -845,3 +846,14 @@ test('Custom bundling file copy variant', () => {
}),
});
});

function findParentTsConfigPath(dir: string, depth: number = 1, limit: number = 5): string {
const target = path.join(dir, 'tsconfig.json');
if (fs.existsSync(target)) {
return target;
} else if (depth < limit) {
return findParentTsConfigPath(path.join(dir, '..'), depth + 1, limit);
}

throw new Error(`No \`package.json\` file found within ${depth} parent directories`);
}
15 changes: 14 additions & 1 deletion packages/@aws-cdk/core/test/runtime-info.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as path from 'path';
import { Construct } from 'constructs';
import { App, NestedStack, Stack, Stage } from '../lib';
Expand Down Expand Up @@ -171,10 +172,22 @@ class TestConstruct extends Construct {
function localCdkVersion(): string {
if (!_cdkVersion) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
_cdkVersion = require(path.join('..', 'package.json')).version;
const pkgJson = findParentPkgJson(__dirname);
_cdkVersion = pkgJson.version;
if (!_cdkVersion) {
throw new Error('Unable to determine CDK version');
}
}
return _cdkVersion;
}

function findParentPkgJson(dir: string, depth: number = 1, limit: number = 5): { version: string; } {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this one, and findParentTsConfigPath, if we need them in another test, let's make sure to extract to a shared utility. I am mostly just writing this comment so I remember about it, I don't think it's necessary now.

const target = path.join(dir, 'package.json');
if (fs.existsSync(target)) {
return JSON.parse(fs.readFileSync(target, 'utf8'));
} else if (depth < limit) {
return findParentPkgJson(path.join(dir, '..'), depth + 1, limit);
}

throw new Error(`No \`package.json\` file found within ${depth} parent directories`);
}
11 changes: 7 additions & 4 deletions tools/@aws-cdk/cfn2ts/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export async function generateAll(
const packagePath = path.join(outPath, moduleName);
const sourcePath = path.join(packagePath, 'lib');

const outputFiles = await generate(moduleScopes, sourcePath, options);
const isCore = moduleName === 'core';
const outputFiles = await generate(moduleScopes, sourcePath, {
...options,
coreImport: isCore ? '.' : options.coreImport,
});

if (!fs.existsSync(path.join(packagePath, 'index.ts'))) {
let lines = moduleScopes.map((s: string) => `// ${s} Cloudformation Resources`);
Expand All @@ -116,10 +120,9 @@ export async function generateAll(
}

// Create .jsiirc.json file if needed
const excludeJsii = ['core'];
if (
!fs.existsSync(path.join(packagePath, '.jsiirc.json'))
&& !excludeJsii.includes(moduleName)
&& !isCore
) {
if (!module) {
throw new Error(
Expand Down Expand Up @@ -180,4 +183,4 @@ async function readScopeMap(filepath: string) : Promise<ModuleMap> {
[name]: { scopes: moduleScopes },
};
}, {});
}
}
105 changes: 41 additions & 64 deletions tools/@aws-cdk/remodel/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as path from 'path';
import {
main as ubergen,
Config,
Export,
LibraryReference,
PackageJson as UbgPkgJson,
} from '@aws-cdk/ubergen';
Expand Down Expand Up @@ -73,6 +72,8 @@ export async function main() {
const templateDir = path.join(__dirname, '..', 'lib', 'template');
await copyTemplateFiles(templateDir, targetDir);

await runBuild(targetDir);

if (clean) {
await fs.remove(path.resolve(targetDir));
}
Expand All @@ -87,9 +88,6 @@ async function makeAwsCdkLib(target: string) {
const pkgJsonPath = path.join(awsCdkLibDir, 'package.json');
const pkgJson: PackageJson = await fs.readJson(pkgJsonPath);

const pkgJsonExports = pkgJson.exports ?? {};


// Local packages that remain unbundled as dev dependencies
const localDevDeps = [
'cdk-build-tools',
Expand Down Expand Up @@ -139,57 +137,55 @@ async function makeAwsCdkLib(target: string) {
};
}, {});

const newPkgJsonExports = formatPkgJsonExports(pkgJsonExports);

// Move all source files into 'lib' to make working on package easier
// Exclude stuff like package.json and other config files
const rootFiles = await fs.readdir(awsCdkLibDir);
const excludeNesting = [
'tsconfig.json',
'.eslintrc.js',
'.gitignore',
'.npmignore',
'LICENSE',
'NOTICE',
'package.json',
'README.md',
'tsconfig.json',
'scripts',
];

await Promise.all(rootFiles.map((file: string) => {
if (excludeNesting.includes(file)) {
return Promise.resolve();
}

const old = path.join(awsCdkLibDir, file);
return fs.move(old, path.join(awsCdkLibDir, 'lib', file));
}));

// Create scope map for codegen usage
await fs.writeJson(
path.join(awsCdkLibDir, 'scripts', 'scope-map.json'),
makeScopeMap(allPackages),
{ spaces: 2 },
);

// Explicitly copy some missing files that ubergen doesn't bring over for various reasons
// Ubergen ignores some of these contents because they are within nested `node_modules` directories
// for testing purposes
await fs.copy(
path.resolve(target, 'packages', '@aws-cdk', 'aws-synthetics', 'test', 'canaries'),
path.resolve(target, 'packages', 'aws-cdk-lib', 'aws-synthetics', 'test', 'canaries'),
{ overwrite: true },
);

await fs.writeJson(pkgJsonPath, {
...pkgJson,
main: 'lib/index.js',
types: 'lib/index.d.ts',
exports: {
...newPkgJsonExports,
'jsii': {
...pkgJson.jsii,
excludeTypescript: [
...pkgJson.jsii.excludeTypescript,
'scripts',
],
},
ubergen: {
'ubergen': {
...pkgJson.ubergen,
libRoot: awsCdkLibDir,
},
scripts: {
'scripts': {
...pkgJson.scripts,
gen: 'ts-node scripts/gen.ts',
build: 'cdk-build',
test: 'jest',
},
devDependencies: {
'cdk-build': {
...pkgJson['cdk-build'],
pre: [
'esbuild --bundle integ-tests/lib/assertions/providers/lambda-handler/index.ts --target=node14 --platform=node --external:aws-sdk --outfile=integ-tests/lib/assertions/providers/lambda-handler.bundle/index.js',
'(cp -f $(node -p \'require.resolve(\"aws-sdk/apis/metadata.json\")\') custom-resources/lib/aws-custom-resource/sdk-api-metadata.json && rm -rf custom-resources/test/aws-custom-resource/cdk.out)',
'(rm -rf core/test/fs/fixtures && cd core/test/fs && tar -xzf fixtures.tar.gz)',
'(rm -rf assets/test/fs/fixtures && cd assets/test/fs && tar -xzvf fixtures.tar.gz)',
],
post: [
'ts-node ./scripts/verify-imports-resolve-same.ts',
'ts-node ./scripts/verify-imports-shielded.ts',
],
},
'devDependencies': {
...filteredDevDeps,
'@aws-cdk/cfn2ts': '0.0.0',
},
Expand All @@ -200,33 +196,14 @@ async function makeAwsCdkLib(target: string) {
// 2. All bundled and deprecated packages
}

// Reformat existing relative path to prepend with "./lib"
function pathReformat(str: string): string {
const split = str.split(/.(.*)/s);
const newVal = ['./lib', split[1]].join('');
return newVal;
}

// Reformat all of the paths in `exports` field of package.json so that they
// correctly include the new `lib` directory.
function formatPkgJsonExports(exports: Record<string, Export>): Record<string, Export> {
const dontFormat = ['./package.json', './.jsii', './.warnings.jsii.js'];
const entries = Object.entries(exports).map(([k, v]) => {
if (typeof v === 'string') {
const newValue = dontFormat.includes(v) ? v : pathReformat(v);
return [k, newValue];
}

const nested = Object.entries(v).map(([nk, nv]) => {
if (nv) {
return [nk, pathReformat(nv)];
} else {return [nk, nv];}
});

return [k, Object.fromEntries(nested)];
});
// Build aws-cdk-lib and the alpha packages
async function runBuild(dir: string) {
const e = (cmd: string, opts: cp.ExecOptions = {}) => exec(cmd, { cwd: dir, ...opts });
await e('yarn install');
// build everything, including all V1 packages so we can transform them if needed
await e('npx lerna run build');

return Object.fromEntries(entries);
await e('./scripts/transform.sh');
}

// Creates a map of directories to the cloudformations scopes that should be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* This build file has two purposes:
* 1. It adds a dependency on each @aws-cdk/aws-xyz package with L1s to this package.
* 2. It generates the file cfn-types-2-classes.json that contains a mapping
* between the CloudFormation type and the fully-qualified name of the L1 class,
* used in the logic of the CfnInclude class.
*/

export async function main() {
console.log('this needs to build the map of CFN resource types to L1s');
}

(async () => {
try {
await main();
} catch (e) {
console.error(e);
process.exit(1);
}
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/jest.config');

module.exports = {
...baseConfig,
testMatch: [
"<rootDir>/**/test/**/?(*.)+(test).js",
],
testEnvironment: 'node',
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { generateAll, ModuleMap } from '@aws-cdk/cfn2ts';
import * as fs from 'fs-extra';
import * as path from 'path';
import { main as genRegionInfoBuiltins } from '../lib/region-info/build-tools/generate-static-data';
import { main as genRegionInfoBuiltins } from '../region-info/build-tools/generate-static-data';
import { main as generateIncludeL1Map } from '../cloudformation-include/build';

const awsCdkLibDir = path.join(__dirname, '..');
const srcDir = path.join(awsCdkLibDir, 'lib');
const pkgJsonPath = path.join(awsCdkLibDir, 'package.json');
const topLevelIndexFilePath = path.join(srcDir, 'index.ts');
const topLevelIndexFilePath = path.join(awsCdkLibDir, 'index.ts');

main()
.then(() => process.exit(0))
Expand All @@ -17,7 +17,7 @@ async function main() {
// Generate all L1s based on config in scope-map.json
const scopeMapPath = path.join(__dirname, 'scope-map.json');

const generated = await generateAll(srcDir, {
const generated = await generateAll(awsCdkLibDir, {
coreImport: '../../core',
cloudwatchImport: '../../aws-cloudwatch',
scopeMapPath,
Expand All @@ -38,8 +38,12 @@ async function main() {

// Call build-tools within modules for other codegen
// TODO: Move these up into aws-cdk-libs/scripts
require('../lib/aws-events-targets/build-tools/gen.js');
require('../aws-events-targets/build-tools/gen.js');
// this can't actually be run because it's dependent of v1 module structure
// and in fact I'm pretty sure that cloudformation-include is just broken
// require('../cloudformation-include/build.js');
await genRegionInfoBuiltins();
await generateIncludeL1Map();
}

async function updatePackageJsonAndIndexFiles(modules: ModuleMap) {
Expand All @@ -66,12 +70,10 @@ async function updatePackageJsonAndIndexFiles(modules: ModuleMap) {
}
}

const exports = [`./${moduleConfig.name}`, `/${moduleConfig.name}`];
exports.forEach((exportName) => {
if (!pkgJson.exports[exportName]) {
pkgJson.exports[exportName] =`./lib/${moduleConfig.name}/index.js`;
}
});
const exportName = `./${moduleConfig.name}`;
if (!pkgJson.exports[exportName]) {
pkgJson.exports[exportName] = `./${moduleConfig.name}/index.js`;
}

if (!topLevelIndexFileEntries.find(e => e.includes(moduleConfig.name))) {
topLevelIndexFileEntries.push(`export * as ${moduleConfig.submodule} from './${moduleConfig.name}';`);
Expand Down
4 changes: 3 additions & 1 deletion tools/@aws-cdk/ubergen/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface PackageJson {
readonly dependencies?: { readonly [name: string]: string };
readonly devDependencies?: { readonly [name: string]: string };
readonly jsii: {
readonly tsc: { [key: string]: unknown },
readonly excludeTypescript: string[],
readonly targets?: {
readonly dotnet?: {
readonly namespace: string;
Expand Down Expand Up @@ -649,7 +651,7 @@ async function rewriteRosettaFixtureImports(fromFile: string, libName: string):
const IGNORED_FILE_NAMES = new Set([
'.eslintrc.js',
'.gitignore',
'.jest.config.js',
'jest.config.js',
'.jsii',
'.npmignore',
'node_modules',
Expand Down