Skip to content

Commit

Permalink
feat(it-tests): test schematics with yarn 1
Browse files Browse the repository at this point in the history
  • Loading branch information
vscaiceanu-1a committed Mar 4, 2024
1 parent cacdc89 commit 73e5b52
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/it-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
packageManager: [yarn, npm]
packageManager: [yarn, npm, yarn1]
testEnvironment: [o3r-project-with-app]
runs-on: ${{ matrix.os }}
env:
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ const prepareWorkspace = (relativeDirectory = '.', projectPackageManager = 'npm'
cwd
}));
};

const isYarn1 = argv['yarn-version']?.startsWith('1');
const addOtterFramework = (relativeDirectory = '.', projectPackageManager = 'npm') => {
const cwd = resolve(process.cwd(), relativeDirectory);
const runner = process.platform === 'win32' ? `${projectPackageManager}.cmd` : projectPackageManager;
const options = schematicsCliOptions
.flat();

exitProcessIfErrorInSpawnSync(3, spawnSync(runner, ['exec', 'ng', 'add', `@o3r/core@~${version}`, ...(projectPackageManager === 'npm' ? ['--'] : []), ...options], {
exitProcessIfErrorInSpawnSync(3, spawnSync(runner, ['exec', 'ng', 'add', `@o3r/core@~${version}`,
...((projectPackageManager === 'npm' || isYarn1) ? ['--'] : []), ...options], {
stdio: 'inherit',
cwd
}));
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/telemetry/src/environment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import { execSync } from 'node:child_process';
import * as path from 'node:path';

/** Support NPM package managers */
type SupportedPackageManagers = 'npm' | 'yarn';
type SupportedPackageManagers = 'npm' | 'yarn' | 'yarn1';

/**
* Determine if the given packager manager is supported
* @param name Name of the package manager
*/
function isSupportedPackageManager(name?: any): name is SupportedPackageManagers {
return name === 'yarn' || name === 'npm';
return name in ['yarn', 'yarn1', 'npm'];
}

/**
* Get package manager used
*/
function getPackageManager() {
if (isSupportedPackageManager(process.env?.ENFORCED_PACKAGE_MANAGER)) {
return process.env.ENFORCED_PACKAGE_MANAGER;
return (process.env.ENFORCED_PACKAGE_MANAGE === 'npm') ? 'npm' : 'yarn';
}
return (process.env?.npm_execpath?.includes('yarn') && 'yarn') || 'npm';
}
Expand Down
9 changes: 4 additions & 5 deletions packages/@o3r/test-helpers/src/prepare-test-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statS
import * as path from 'node:path';
import type { PackageJson } from 'type-fest';
import { createTestEnvironmentBlank } from './test-environments/create-test-environment-blank';
import { createWithLock, getPackageManager, type Logger, packageManagerInstall, setPackagerManagerConfig, setupGit } from './utilities/index';
import { createWithLock, getVersionedPackageManager, isYarn1, type Logger, packageManagerInstall, setPackagerManagerConfig, setupGit, YARN_1_LATEST_VERSION } from './utilities/index';
import { createTestEnvironmentOtterProjectWithApp } from './test-environments/create-test-environment-otter-project';
import { O3rCliError } from '@o3r/schematics';

Expand All @@ -15,8 +15,7 @@ export type PrepareTestEnvType = 'blank' | 'o3r-project-with-app';

/**
* Retrieve the version used by yarn and setup at root level
* @param rootFolderPath: path to the folder where to take the configuration from
* @param rootFolderPath
* @param rootFolderPath path to the folder where to take the configuration from
*/
export function getYarnVersionFromRoot(rootFolderPath: string) {
const o3rPackageJson: PackageJson & { generatorDependencies?: Record<string, string> } =
Expand Down Expand Up @@ -49,7 +48,7 @@ export async function prepareTestEnv(folderName: string, options?: PrepareTestEn
const cacheFolderPath = path.resolve(globalFolderPath, 'cache');

JSON.parse(readFileSync(path.join(rootFolderPath, 'packages', '@o3r', 'core', 'package.json')).toString());
const yarnVersion: string = yarnVersionParam || getYarnVersionFromRoot(rootFolderPath);
const yarnVersion: string = yarnVersionParam || (isYarn1() && YARN_1_LATEST_VERSION) || getYarnVersionFromRoot(rootFolderPath);
const execAppOptions: ExecSyncOptions = {
cwd: workspacePath,
stdio: 'inherit',
Expand Down Expand Up @@ -108,7 +107,7 @@ export async function prepareTestEnv(folderName: string, options?: PrepareTestEn
let isInWorkspace = false;
let untouchedProject: undefined | string;
let untouchedProjectPath: undefined | string;
const appDirectory = `${type}-${getPackageManager()}`;
const appDirectory = `${type}-${getVersionedPackageManager()}`;
switch (type) {
case 'blank': {
await createTestEnvironmentBlank({
Expand Down
57 changes: 47 additions & 10 deletions packages/@o3r/test-helpers/src/utilities/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const PACKAGE_MANAGERS_CMD = {
run: ['yarn', 'run'],
workspaceExec: ['yarn', 'workspace'],
workspaceRun: ['yarn', 'workspace']
},
yarn1: {
add: ['yarn', 'add'],
create: ['yarn', 'create'],
exec: ['yarn'],
install: ['yarn', 'install'],
run: ['yarn', 'run'],
workspaceExec: ['yarn', 'workspace'],
workspaceRun: ['yarn', 'workspace']
}
};

Expand All @@ -41,14 +50,36 @@ type CommandArguments = {
};

/**
* Get the package manager to be used for the tests by reading environment variable ENFORCED_PACKAGE_MANAGER
* Get the package manager with its version to be used for the tests by reading environment variable ENFORCED_PACKAGE_MANAGER
* 'yarn', 'yarn1', 'npm'
*/
export function getPackageManager() {
export function getVersionedPackageManager() {
return (process.env.ENFORCED_PACKAGE_MANAGER && process.env.ENFORCED_PACKAGE_MANAGER in PACKAGE_MANAGERS_CMD) ?
process.env.ENFORCED_PACKAGE_MANAGER as keyof typeof PACKAGE_MANAGERS_CMD :
'yarn';
}

/**
* Get the package manager to be used for the tests based on env ENFORCED_PACKAGE_MANAGER
* 'yarn', 'npm'
*/
export function getPackageManager() {
return getVersionedPackageManager() === 'npm' ? 'npm' : 'yarn';
}


/**
* latest version of yarn 1
*/
export const YARN_1_LATEST_VERSION = '1.22.19';

/**
* is yarn 1
*/
export function isYarn1() {
return process.env.ENFORCED_PACKAGE_MANAGER === 'yarn1';
}

/**
* Need to add additional dashes when running command like exec on npm
* Convert `npm exec test --param` to `npm exec test -- --param`
Expand Down Expand Up @@ -90,7 +121,7 @@ function execCmd(args: string[], execOptions: ExecSyncOptions) {
* @param options
*/
export function packageManagerAdd(packages: string, options: ExecSyncOptions) {
return execCmd([...PACKAGE_MANAGERS_CMD[getPackageManager()].add, packages], options);
return execCmd([...PACKAGE_MANAGERS_CMD[getVersionedPackageManager()].add, packages], options);
}

/**
Expand All @@ -101,7 +132,7 @@ export function packageManagerAdd(packages: string, options: ExecSyncOptions) {
*/
export function packageManagerCreate(command: CommandArguments, options: ExecSyncOptions, packageManagerOverride?: keyof typeof PACKAGE_MANAGERS_CMD) {
const { script, args } = command;
const packageManager = packageManagerOverride || getPackageManager();
const packageManager = packageManagerOverride || getVersionedPackageManager();
return execCmd([...PACKAGE_MANAGERS_CMD[packageManager].create, script, ...addDashesForNpmCommand(args, packageManager)], options);
}

Expand All @@ -112,7 +143,7 @@ export function packageManagerCreate(command: CommandArguments, options: ExecSyn
*/
export function packageManagerExec(command: CommandArguments, options: ExecSyncOptions) {
const { script, args } = command;
return execCmd([...PACKAGE_MANAGERS_CMD[getPackageManager()].exec, script, ...addDashesForNpmCommand(args)], options);
return execCmd([...PACKAGE_MANAGERS_CMD[getVersionedPackageManager()].exec, script, ...addDashesForNpmCommand(args)], options);
}

/**
Expand All @@ -123,7 +154,7 @@ export function packageManagerExec(command: CommandArguments, options: ExecSyncO
*/
export function packageManagerWorkspaceExec(workspaceProjectName: string, command: CommandArguments, options: ExecSyncOptions) {
const { script, args } = command;
return execCmd([...PACKAGE_MANAGERS_CMD[getPackageManager()].workspaceExec, workspaceProjectName, script, ...addDashesForNpmCommand(args)], options);
return execCmd([...PACKAGE_MANAGERS_CMD[getVersionedPackageManager()].workspaceExec, workspaceProjectName, script, ...addDashesForNpmCommand(args)], options);
}

/**
Expand All @@ -142,7 +173,7 @@ export function packageManagerExecOnProject(projectName: string, isInWorkspace:
* @param options
*/
export function packageManagerInstall(options: ExecSyncOptions) {
return execCmd(PACKAGE_MANAGERS_CMD[getPackageManager()].install, options);
return execCmd(PACKAGE_MANAGERS_CMD[getVersionedPackageManager()].install, options);
}

/**
Expand All @@ -152,7 +183,7 @@ export function packageManagerInstall(options: ExecSyncOptions) {
*/
export function packageManagerRun(command: CommandArguments, options: ExecSyncOptions) {
const { script, args } = command;
return execCmd([...PACKAGE_MANAGERS_CMD[getPackageManager()].run, script, ...addDashesForNpmCommand(args)], options);
return execCmd([...PACKAGE_MANAGERS_CMD[getVersionedPackageManager()].run, script, ...addDashesForNpmCommand(args)], options);
}

/**
Expand All @@ -163,7 +194,7 @@ export function packageManagerRun(command: CommandArguments, options: ExecSyncOp
*/
export function packageManagerWorkspaceRun(workspaceProjectName: string, command: CommandArguments, options: ExecSyncOptions) {
const { script, args } = command;
return execCmd([...PACKAGE_MANAGERS_CMD[getPackageManager()].workspaceRun, workspaceProjectName, script, ...addDashesForNpmCommand(args)], options);
return execCmd([...PACKAGE_MANAGERS_CMD[getVersionedPackageManager()].workspaceRun, workspaceProjectName, script, ...addDashesForNpmCommand(args)], options);
}

/**
Expand Down Expand Up @@ -209,7 +240,7 @@ export function setPackagerManagerConfig(options: PackageManagerConfig, execAppO

const packageJsonPath = join(execOptions.cwd as string, 'package.json');
const shouldCleanPackageJson = !existsSync(packageJsonPath);
switch (getPackageManager()) {
switch (getVersionedPackageManager()) {
case 'yarn': {
// Set yarn version
if (options.yarnVersion) {
Expand All @@ -231,6 +262,12 @@ export function setPackagerManagerConfig(options: PackageManagerConfig, execAppO
execFileSync('yarn', ['config', 'set', 'unsafeHttpWhitelist', '127.0.0.1'], execOptions);
break;
}
case 'yarn1':{
execFileSync('yarn', ['config', 'set', '--add.ignore-workspace-root-check', 'true'], execOptions);

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application with extractors › should add extractors to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at lockFilePath (../test-helpers/src/prepare-test-env.ts:86:31) at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:84:25) at Object.<anonymous> (schematics/index.it.spec.ts:23:96)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application › should build empty app

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:29:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application with testing › should add testing to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:25:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application with rules-engine › should add rules engine to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:25:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application with analytics › should add analytics to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:25:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application with components › should add components to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:23:96)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn1, o3r-project-with-app)

new otter application with Design › should add design to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:23:74)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application › should build empty app

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at lockFilePath (../test-helpers/src/prepare-test-env.ts:86:31) at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:84:25) at Object.<anonymous> (schematics/index.it.spec.ts:29:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application with extractors › should add extractors to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:23:96)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application with testing › should add testing to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:25:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application with rules-engine › should add rules engine to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:25:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application with analytics › should add analytics to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:25:109)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application with components › should add components to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:23:96)

Check failure on line 266 in packages/@o3r/test-helpers/src/utilities/package-manager.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn1, o3r-project-with-app)

new otter application with Design › should add design to existing application

Command failed: yarn config set --add.ignore-workspace-root-check true at setPackagerManagerConfig (../test-helpers/src/utilities/package-manager.ts:266:19) at ../test-helpers/src/test-environments/create-test-environment-otter-project.ts:74:29 at createWithLock (../test-helpers/src/utilities/create-with-lock.ts:80:11) at createTestEnvironmentOtterProjectWithApp (../test-helpers/src/test-environments/create-test-environment-otter-project.ts:59:23) at prepareTestEnv (../test-helpers/src/prepare-test-env.ts:126:53) at Object.<anonymous> (schematics/index.it.spec.ts:23:74)
if (options.yarnVersion) {
execFileSync('yarn', ['set', 'version', options.yarnVersion], execOptions);
}
}
}

execFileSync('npm', ['config', 'set', 'audit=false', '-L=project'], execOptions);
Expand Down

0 comments on commit 73e5b52

Please sign in to comment.