From d9341701ab2c0fa2b622fb113505c1f2c3d15f12 Mon Sep 17 00:00:00 2001 From: Yoann Moinet Date: Thu, 3 Oct 2024 15:00:50 +0200 Subject: [PATCH] WIP Test --- .../src/commands/create-plugin/index.test.ts | 80 +++++++++++++++++++ .../src/commands/create-plugin/templates.ts | 1 + packages/tools/src/helpers.ts | 2 + 3 files changed, 83 insertions(+) create mode 100644 packages/tests/src/tools/src/commands/create-plugin/index.test.ts diff --git a/packages/tests/src/tools/src/commands/create-plugin/index.test.ts b/packages/tests/src/tools/src/commands/create-plugin/index.test.ts new file mode 100644 index 00000000..831bba92 --- /dev/null +++ b/packages/tests/src/tools/src/commands/create-plugin/index.test.ts @@ -0,0 +1,80 @@ +// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2019-Present Datadog, Inc. + +import commands from '@dd/tools/commands/create-plugin/index'; +import { ROOT } from '@dd/tools/constants'; +import { Cli } from 'clipanion'; +import fs from 'fs'; +import { vol } from 'memfs'; +import path from 'path'; + +jest.mock('fs', () => require('memfs').fs); + +const getMirroredFixtures = (paths: string[], cwd: string) => { + const fsa = jest.requireActual('fs'); + const fixtures: Record = {}; + for (const p of paths) { + fixtures[p] = fsa.readFileSync(path.resolve(cwd, p), 'utf-8'); + } + return fixtures; +}; + +const getArgs = (opts: { + name?: string; + description?: string; + codeowners?: string[]; + type?: string; + hooks?: string[]; +}) => { + const args: string[] = []; + if (opts.name) { + args.push('--name', opts.name); + } + if (opts.description) { + args.push('--description', opts.description); + } + if (opts.codeowners) { + args.push(...opts.codeowners.map((co) => ['--codeowner', co]).flat()); + } + if (opts.type) { + args.push('--type', opts.type); + } + if (opts.hooks) { + args.push(...opts.hooks.map((h) => ['--hook', h]).flat()); + } + return args; +}; + +describe('Command create-plugin', () => { + const fixtures = getMirroredFixtures( + ['.github/CODEOWNERS', `packages/plugins/telemetry/package.json`], + ROOT, + ); + console.log('FIXTURES', Object.keys(fixtures)); + beforeEach(() => { + // Mock the files that are touched by yarn cli create-plugin and yarn cli integrity. + vol.fromJSON(fixtures, ROOT); + }); + + afterEach(() => { + vol.reset(); + }); + + test('It should create a plugin.', async () => { + const cli = new Cli(); + cli.register(commands[0]); + + const args = getArgs({ + name: 'Testing #1', + description: 'Testing plugin', + codeowners: ['@codeowners'], + type: 'universal', + hooks: ['enforce'], + }); + const result = await cli.run(['create-plugin', ...args]); + console.log('TEST', fs.readdirSync(ROOT)); + console.log(result); + // console.log(Object.keys(vol.toJSON()).map((k) => k.replace(ROOT, '.'))); + }); +}); diff --git a/packages/tools/src/commands/create-plugin/templates.ts b/packages/tools/src/commands/create-plugin/templates.ts index 9ab60350..7e67a8d2 100644 --- a/packages/tools/src/commands/create-plugin/templates.ts +++ b/packages/tools/src/commands/create-plugin/templates.ts @@ -17,6 +17,7 @@ const getTemplates = (context: Context): File[] => { const pascalCase = getPascalCase(plugin.slug); const camelCase = getCamelCase(plugin.slug); const pkg = getPackageJsonData(); + console.log('Got package.json data:', pkg); return [ { diff --git a/packages/tools/src/helpers.ts b/packages/tools/src/helpers.ts index 05bb8aa2..8df498aa 100644 --- a/packages/tools/src/helpers.ts +++ b/packages/tools/src/helpers.ts @@ -6,6 +6,7 @@ import type { GetPlugins } from '@dd/core/types'; import chalk from 'chalk'; import { execFile } from 'child_process'; import fs from 'fs-extra'; +import fsa from 'fs'; import path from 'path'; import { promisify } from 'util'; @@ -59,6 +60,7 @@ export const getCamelCase = (name: string): string => { }; export const getPackageJsonData = (workspace: string = 'plugins/telemetry'): any => { + console.log('COMMAND', fsa.readdirSync(ROOT)); const packageJson = fs.readJSONSync(path.resolve(ROOT, `packages/${workspace}/package.json`)); return packageJson; };