Skip to content

Commit

Permalink
progress env
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 2, 2024
1 parent 6890726 commit 3d5ceb8
Show file tree
Hide file tree
Showing 30 changed files with 1,178 additions and 193 deletions.
1 change: 1 addition & 0 deletions .verdaccio/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# path to a directory with all packages
# overwritten by --storage option
storage: ../tmp/local-registry/storage

# a list of other known repositories we can talk to
Expand Down
34 changes: 34 additions & 0 deletions e2e/cli-e2e-env/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# cli-e2e-env

End-to-end tests for the `cli` library.

### Changes/generated files during e2e (already refactored to multiple verdaccio instances)

```sh
Root/ # 👈 this is your CWD
├── dist/
│ └── packages/
│ └── <project-name>/...
└── tmp/
└── e2e/
└── <project-name>/ # e2e setup
├── storage/... # npm publish/unpublish
├── node_modules/
│ └── <org>
│ └── <package-name>/... # npm install/uninstall
├── __test__/...
│ └── <file-name>/... # e2e beforeEach
│ └── <it-block-setup>/...
├── .npmrc # local npm config configured for project specific verdaccio registry
├── package-lock.json # npm install/uninstall
└── package.json # npm install/uninstall
```

### Troubleshooting

- `nx start-server`
- `nx start-server <project-name>`
- `nx start-server <project-name> --storage tmp/e2e/<project-name>/storage`
- `nx start-env`
- `nx start-env <project-name> --workspaceRoot tmp/e2e/<project-name>`
- `nx npm-publish <project-name> --envProject=<env-project-name>`
- `nx run-many -t npm-publish --envProjectName=cli-e2e-env`
- `nx npm-install <project-name> --envProject=<env-project-name>`
- `nx run-many -t npm-install --envProjectName=cli-e2e-env`
1 change: 1 addition & 0 deletions e2e/cli-e2e-env/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
"options": {
"globalSetup": ["e2e/cli-e2e-env/setup/global-setup.ts"],
"reportsDirectory": "../../coverage/projects/cli-e2e-env"
}
}
Expand Down
87 changes: 47 additions & 40 deletions e2e/cli-e2e-env/setup/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,75 @@
import { executeProcess } from '@org/test-utils';
import { NpmTestEnvResult, startNpmEnv } from '../../../tools/utils/env';
import { executeProcess, objectToCliArgs } from '@org/test-utils';
import { NpmTestEnvResult } from '../../../tools/utils/env';
import { join, relative } from 'node:path';
import { rm } from 'node:fs/promises';
import { VerdaccioExecuterOptions } from '../../../tools/utils/registry';
import { readJsonFile } from '@nx/devkit';

const isVerbose: boolean = process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
const teardownEnv: boolean = process.env.E2E_TEARDOWN_ENV !== 'false';
// DEBUG FLAGS
const isVerbose: boolean = true; // process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
const teardownEnv: boolean = true; //process.env.E2E_TEARDOWN_ENV !== 'false';
const teardownRegistry: boolean = true; //process.env.E2E_TEARDOWN_REGISTRY !== 'false';

let activeRegistry: NpmTestEnvResult;
const projectName = process.env['NX_TASK_TARGET_PROJECT'];
let stopRegistry;

export async function setup() {
// start registry
activeRegistry = await startNpmEnv({
projectName,
workspaceRoot: join('tmp', 'e2e', projectName),
targetName: 'start-verdaccio',
await executeProcess({
command: 'nx',
args: objectToCliArgs<
Partial<
VerdaccioExecuterOptions & {
_: string[];
}
>
>({
_: ['start-env', projectName ?? '', '--'],
verbose: isVerbose,
clear: true,
readyWhen: 'Environment ready under',
}),
verbose: isVerbose,
clear: true,
shell: true,
});

const { registry, workspaceRoot, stop } = activeRegistry;
stopRegistry = stop;
const workspaceRoot = join('tmp', 'npm-env', projectName);
activeRegistry = await readJsonFile(
join(workspaceRoot, 'verdaccio-registry.json')
);
const { registry } = activeRegistry;
stopRegistry = () => process.kill(Number(registry.pid));

const { url } = registry;
const userconfig = join(workspaceRoot, '.npmrc');
// package publish all projects
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-publish', `--userconfig=${userconfig}`],
observer: {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout);
}
},
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout);
}
},
},
args: objectToCliArgs({
_: ['run-many', '-t=npm-publish'],
verbose: isVerbose,
envProjectName: projectName,
}),
verbose: isVerbose,
});

// package install all projects to test env folder
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-install', `--prefix=${workspaceRoot}`],
observer: {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout);
}
},
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout);
}
},
},
args: objectToCliArgs({
_: ['run-many', '-t=npm-install'],
verbose: isVerbose,
envProjectName: projectName,
}),
verbose: isVerbose,
});
}

export async function teardown() {
console.table(activeRegistry);
stopRegistry();
// teardownEnv && await rm(activeRegistry.workspaceRoot, {recursive: true, force: true});
if (teardownRegistry) {
}
await rm(activeRegistry.workspaceRoot, { recursive: true, force: true });
if (teardownEnv) {
}
}
24 changes: 18 additions & 6 deletions e2e/cli-e2e-env/test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { execSync } from 'node:child_process';
import { mkdir, rm, writeFile } from 'node:fs/promises';
import { executeProcess, objectToCliArgs } from '@org/test-utils';

describe('CLI command - sort', () => {
const baseDir = 'tmp/cli-e2e-env/sort';
const workspaceRoot = 'tmp/npm-env/cli-e2e-env';
const baseDir = join(workspaceRoot, 'sort');
const userconfig = join(baseDir, '.npmrc');

afterEach(async () => {
// await rm(baseDir, {recursive: true, force: true});
await rm(baseDir, { recursive: true, force: true });
});

it('should execute CLI command sort when param file is given', async () => {
Expand All @@ -18,8 +20,18 @@ describe('CLI command - sort', () => {
JSON.stringify([{ name: 'Michael' }, { name: 'Alice' }])
);

expect(() => execSync(`npx @org/cli sort --file="${testPath}"`)).toThrow(
'Command failed: npx @org/cli sort --file="tmp/cli/sort/file-sort/users.json"'
await expect(
executeProcess({
command: 'npx',
args: objectToCliArgs({
_: ['@org/cli', 'sort'],
file: testPath,
}),
cwd: workspaceRoot,
verbose: true,
})
).rejects.toThrow(
'The "path" argument must be of type string or an instance of Buffer or URL'
);
/*
const content = (await readFile(testPath)).toString();
Expand Down
30 changes: 30 additions & 0 deletions e2e/cli-e2e-graph/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": ["../../.eslintrc.json", "../../.eslintrc.base.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredFiles": ["{projectRoot}/vite.config.{js,ts,mjs,mts}"]
}
]
}
}
]
}
15 changes: 15 additions & 0 deletions e2e/cli-e2e-graph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# cli-e2e-graph

End-to-end tests for the `cli` library.

### Troubleshooting

- `nx start-server`
- `nx start-server <project-name>`
- `nx start-server <project-name> --storage tmp/e2e/<project-name>/storage`
- `nx start-env`
- `nx start-env <project-name> --workspaceRoot tmp/e2e/<project-name>`
- `nx npm-publish <project-name> --envProject=<env-project-name>`
- `nx run-many -t npm-publish --envProjectName=cli-e2e-graph`
- `nx npm-install <project-name> --envProject=<env-project-name>`
- `nx run-many -t npm-install --envProjectName=cli-e2e-graph`
18 changes: 18 additions & 0 deletions e2e/cli-e2e-graph/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "cli-e2e-graph",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "projects/cli-e2e-graph/test",
"projectType": "library",
"tags": ["npm-env"],
"targets": {
"e2e": {
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
"options": {
"globalSetup": ["e2e/cli-e2e-graph/setup/global-setup.ts"],
"reportsDirectory": "../../coverage/projects/cli-e2e-graph"
}
}
}
}
86 changes: 86 additions & 0 deletions e2e/cli-e2e-graph/setup/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { executeProcess, objectToCliArgs } from '@org/test-utils';
import { NpmTestEnvResult } from '../../../tools/utils/env';
import { join, relative } from 'node:path';
import { rm } from 'node:fs/promises';
import { VerdaccioExecuterOptions } from '../../../tools/utils/registry';
import { readJsonFile } from '@nx/devkit';

const isVerbose: boolean = process.env.NX_VERBOSE_LOGGING === 'true' ?? false;
const teardownEnv: boolean = process.env.E2E_TEARDOWN_ENV !== 'false';

let activeRegistry: NpmTestEnvResult;
const projectName = process.env['NX_TASK_TARGET_PROJECT'];
let stopRegistry;

export async function setup() {
// start registry
await executeProcess({
command: 'nx',
args: objectToCliArgs<
Partial<
VerdaccioExecuterOptions & {
_: string[];
verbose: boolean;
cwd: string;
}
>
>({
_: ['start-verdaccio', projectName ?? ''],
workspaceRoot: join('tmp', 'npm-env', projectName),
verbose: isVerbose,
clear: true,
}),
shell: true,
});

activeRegistry = await readJsonFile(
join('tmp', 'npm-env', projectName, 'verdaccio-registry.json')
);
const { registry, workspaceRoot, stop } = activeRegistry;
stopRegistry = process.kill(Number(registry.pid));

const { url } = registry;
const userconfig = join(workspaceRoot, '.npmrc');
// package publish all projects
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-publish', `--userconfig=${userconfig}`],
observer: {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout);
}
},
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout);
}
},
},
});

// package install all projects to test env folder
await executeProcess({
command: 'nx',
args: ['run-many', '-t=npm-install', `--prefix=${workspaceRoot}`],
observer: {
onStdout: (stdout) => {
if (isVerbose) {
console.info(stdout);
}
},
onStderr: (stdout) => {
if (isVerbose) {
console.error(stdout);
}
},
},
});
}

export async function teardown() {
const s = readJsonFile('');
stopRegistry();
teardownEnv &&
(await rm(activeRegistry.workspaceRoot, { recursive: true, force: true }));
}
32 changes: 32 additions & 0 deletions e2e/cli-e2e-graph/test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { execSync } from 'node:child_process';

describe('CLI command - sort', () => {
const baseDir = 'tmp/e2e/cli-e2e-graph/sort';

afterEach(async () => {
// await rm(baseDir, {recursive: true, force: true});
});

it('should execute CLI command sort when param file is given', async () => {
const testPath = join(baseDir, 'file-sort', 'users.json');
await mkdir(dirname(testPath), { recursive: true });
await writeFile(
testPath,
JSON.stringify([{ name: 'Michael' }, { name: 'Alice' }])
);

expect(() => execSync(`npx @org/cli sort --file="${testPath}"`)).toThrow(
'Command failed: npx @org/cli sort --file="tmp/cli-e2e-graph/sort/file-sort/users.json"'
);
/*
const content = (await readFile(testPath)).toString();
expect(JSON.parse(content)).toEqual([
{name: 'Alice'},
{name: 'Michael'},
]);
*/
});
});
Loading

0 comments on commit 3d5ceb8

Please sign in to comment.