UNPKG

2.61 kBPlain TextView Raw
1import { execSync } from 'child_process';
2import { join, resolve } from 'path';
3
4import * as fsExtra from 'fs-extra';
5
6export const ROOT_DIR = resolve(join(__dirname, '..'));
7const CI_TOOLS_EXECUTABLE = join(ROOT_DIR, 'dist', 'ci_tools.js');
8
9export function run(ciToolsCommand: string): string {
10 return shell(`node ${CI_TOOLS_EXECUTABLE} ${ciToolsCommand}`);
11}
12
13export function shell(shellCommand: string): string {
14 console.log(' shell:', shellCommand);
15
16 const env = { ...process.env };
17 delete env['GIT_BRANCH'];
18 delete env['GITHUB_REF'];
19
20 const output = execSync(`${shellCommand}`, { encoding: 'utf-8', env: env });
21
22 return output;
23}
24
25/**
26 * Changes the current working directory (cwd) to the given `directory` and executes the given `callback`.
27 * Resets the cwd afterwards.
28 *
29 * Example:
30 *
31 * await inDir('test/fixtures/node-simple', async () => {
32 * const packageVersion = await getPackageVersion('dotnet');
33 * assert.equal(packageVersion, '1.2.3');
34 * });
35 */
36export async function inDir(directory: string, callback: () => Promise<void>): Promise<void> {
37 const oldDir = process.cwd();
38 try {
39 process.chdir(directory);
40 await callback.apply(null);
41 } catch (err) {
42 console.error(`inDir(${directory}): ${err}`);
43 throw err;
44 } finally {
45 process.chdir(oldDir);
46 }
47}
48
49export function inDirSync(directory: string, callback: () => void): void {
50 const oldDir = process.cwd();
51 try {
52 process.chdir(directory);
53 callback.apply(null);
54 } catch (err) {
55 console.error(`inDir(${directory}): ${err}`);
56 throw err;
57 } finally {
58 process.chdir(oldDir);
59 }
60}
61
62/**
63 * Sets up a working copy of a sample repo, including a Git remote that integration tests can safely push to.
64 */
65export function setupGitWorkingCopyForTest(): string {
66 const gitRemoteForTest = createGitRemoteToCloneFrom();
67 const gitTempWorkingCopy = cloneTestRepoFromRemote(gitRemoteForTest);
68 return gitTempWorkingCopy;
69}
70
71function createGitRemoteToCloneFrom(): string {
72 // this is a Git bare repo that we can easily clone locally for testing purposes
73 const gitFixtureDir = resolve(join(ROOT_DIR, 'test', 'fixtures', 'simple.git'));
74 const newGitRemote = resolve(join(ROOT_DIR, 'tmp', 'origin'));
75
76 fsExtra.removeSync(newGitRemote);
77 fsExtra.copySync(gitFixtureDir, newGitRemote);
78
79 return newGitRemote;
80}
81
82function cloneTestRepoFromRemote(remote: string): string {
83 const workingCopyDir = resolve(join(ROOT_DIR, 'tmp', 'working-copy'));
84
85 fsExtra.removeSync(workingCopyDir);
86 shell(`git clone ${remote} ${workingCopyDir}`);
87
88 return workingCopyDir;
89}