Skip to content

Commit

Permalink
fix: split create its
Browse files Browse the repository at this point in the history
  • Loading branch information
mrednic-1A committed Mar 15, 2024
1 parent e7bf5b3 commit fd8f9ca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/@ama-sdk/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Create new sdk command', () => {
setupLocalRegistry();
beforeEach(async () => {
const isYarnTest = packageManager.startsWith('yarn');
const testEnv = await prepareTestEnv(projectName, { type: 'blank' });
const testEnv = await prepareTestEnv(projectName, { type: 'blank', packageScope: 'ama-sdk' });
sdkFolderPath = testEnv.workspacePath;
const yarnVersion = testEnv.packageManagerConfig.yarnVersion;
sdkPackagePath = path.join(sdkFolderPath, sdkPackageName.replace(/^@/, ''));
Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const execWorkspaceOptions = getDefaultExecSyncOptions();
describe('Create new otter project command', () => {
setupLocalRegistry();
beforeEach(async () => {
({ workspacePath, packageManagerConfig, workspacePath } = (await prepareTestEnv(appFolder, {type: 'blank' })));
({ workspacePath, packageManagerConfig, workspacePath } = (await prepareTestEnv(appFolder, {type: 'blank', packageScope: 'o3r' })));
execWorkspaceOptions.cwd = workspacePath;
});

Expand Down
14 changes: 10 additions & 4 deletions packages/@o3r/test-helpers/src/prepare-test-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface PrepareTestEnvOptions {
yarnVersion?: string;
/** Logger to use for logging */
logger?: Logger;
/**
* Scope of package used to run the create command
* Used on yarn1 tests, because it is not able to differetiate from which package scope to run the create
*/
packageScope?: string;
}

/**
Expand All @@ -42,7 +47,7 @@ export async function prepareTestEnv(folderName: string, options?: PrepareTestEn
const logger = options?.logger || console;
const yarnVersionParam = options?.yarnVersion;
const rootFolderPath = process.cwd();
const itTestsFolderPath = path.resolve(rootFolderPath, '..', 'it-tests');
const itTestsFolderPath = path.resolve(rootFolderPath, '..', 'it-tests', options?.packageScope || '');
const workspacePath = path.resolve(itTestsFolderPath, folderName);
const globalFolderPath = path.resolve(rootFolderPath, '.cache', 'test-app');
const cacheFolderPath = path.resolve(globalFolderPath, 'cache');
Expand Down Expand Up @@ -76,17 +81,18 @@ export async function prepareTestEnv(folderName: string, options?: PrepareTestEn
const packageManagerConfig = {
yarnVersion,
globalFolderPath,
registry: 'http://127.0.0.1:4873'
registry: 'http://127.0.0.1:4873',
...(isYarn1Enforced() ? {packageScope: options?.packageScope} : {})
};

// Create it-tests folder
if (!existsSync(itTestsFolderPath)) {
logger.debug?.(`Creating it-tests folder`);
await createWithLock(() => {
mkdirSync(itTestsFolderPath);
mkdirSync(itTestsFolderPath, { recursive: true });
setPackagerManagerConfig(packageManagerConfig, {...execAppOptions, cwd: itTestsFolderPath});
return Promise.resolve();
}, {lockFilePath: `${itTestsFolderPath}.lock`, cwd: path.join(rootFolderPath, '..'), appDirectory: 'it-tests'});
}, {lockFilePath: `${itTestsFolderPath}.lock`, cwd: path.join(rootFolderPath, '..'), appDirectory: path.join('it-tests', options?.packageScope || '')});
}

// Remove existing app
Expand Down
18 changes: 17 additions & 1 deletion packages/@o3r/test-helpers/src/utilities/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execFileSync, ExecSyncOptions } from 'node:child_process';
import { appendFileSync, existsSync, readFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { join, posix, sep } from 'node:path';
import { performance } from 'node:perf_hooks';

declare global {
Expand Down Expand Up @@ -217,6 +217,12 @@ export interface PackageManagerConfig {
* Custom registry used to fetch local packages
*/
registry: string;

/**
* Scope of package used to run the create command
* Used on yarn1 tests, because it is not able to differetiate from which package scope to run the create
*/
packageScope?: string;
}

/**
Expand Down Expand Up @@ -264,6 +270,8 @@ export function setPackagerManagerConfig(options: PackageManagerConfig, execAppO
execFileSync('yarn', ['config', 'set', '@o3r:registry', options.registry], execOptions);
execFileSync('yarn', ['config', 'set', 'unsafeHttpWhitelist', '127.0.0.1'], execOptions);
}
const cacheFolder = 'cache-folder';
const globalFolder = 'global-folder';
const ignoreRootCheckConfig = '--add.ignore-workspace-root-check';
const yarnRcPath = join(execOptions.cwd as string, '.yarnrc');

Expand All @@ -272,6 +280,14 @@ export function setPackagerManagerConfig(options: PackageManagerConfig, execAppO
if (!content.includes(ignoreRootCheckConfig)) {
appendFileSync(yarnRcPath, `\n${ignoreRootCheckConfig} true`);
}
if (options.globalFolderPath) {
if (!content.includes(cacheFolder)) {
appendFileSync(yarnRcPath, `\n${cacheFolder} "${posix.join(options.globalFolderPath.split(sep).join(posix.sep), 'yarn1-cache', options.packageScope || '')}"`);
}
if (!content.includes(globalFolder)) {
appendFileSync(yarnRcPath, `\n${globalFolder} "${posix.join(options.globalFolderPath.split(sep).join(posix.sep), 'yarn1-global', options.packageScope || '')}"`);
}
}
} else {
console.warn(`File not found at '${yarnRcPath}'.`);
}
Expand Down

0 comments on commit fd8f9ca

Please sign in to comment.