Skip to content

Commit

Permalink
WIP Test
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Oct 3, 2024
1 parent 27853b3 commit d934170
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
80 changes: 80 additions & 0 deletions packages/tests/src/tools/src/commands/create-plugin/index.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {};
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, '.')));
});
});
1 change: 1 addition & 0 deletions packages/tools/src/commands/create-plugin/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand Down
2 changes: 2 additions & 0 deletions packages/tools/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit d934170

Please sign in to comment.