Skip to content

Commit 5a85e33

Browse files
feat(test:downstream): Move downstream projects into temp dir before running tests
1 parent 40ce48b commit 5a85e33

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"readline-sync": "^1.4.7",
3636
"shelljs": "^0.7.8",
3737
"shx": "^0.2.2",
38+
"tmp": "^0.0.33",
3839
"tweak-sourcemap-paths": "0.0.3",
3940
"yargs": "^9.0.1"
4041
}

test_downstream_projects.js

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
22
const fs = require('fs');
33
const path = require('path');
4+
const tmp = require('tmp');
5+
const shelljs = require('shelljs');
46

57
const publishYalcPackage = require('./publish_yalc_package');
68
const util = require('./util');
@@ -10,7 +12,10 @@ const PKG_DIR = process.cwd();
1012
const config = JSON.parse(fs.readFileSync('downstream_projects.json'));
1113
const pkgjson = JSON.parse(fs.readFileSync('package.json'));
1214

13-
const DOWNSTREAMS_PATH = path.resolve(PKG_DIR, '.downstream_cache');
15+
const DOWNSTREAM_CACHE = path.resolve(PKG_DIR, '.downstream_cache');
16+
const TEMP = tmp.dirSync();
17+
const TEMP_DIR = TEMP.name;
18+
1419
const UPSTREAM_PKGS = (process.env.UPSTREAM_PKGS || '').split(',').filter(x => x).concat(pkgjson.name);
1520
const DOWNSTREAM_PKGS = (process.env.DOWNSTREAM_PKGS || '').split(',').filter(x => x);
1621

@@ -21,16 +26,15 @@ function forEachDownstream(callback) {
2126
return;
2227
}
2328

24-
process.chdir(path.resolve(DOWNSTREAMS_PATH, key));
29+
process.chdir(path.resolve(DOWNSTREAM_CACHE, key));
2530
callback(key, config[key]);
2631
});
2732
}
2833

29-
function makeWorkingCopy() {
30-
process.chdir(PKG_DIR);
31-
if (!fs.existsSync(DOWNSTREAMS_PATH)) {
34+
function makeDownstreamCache() {
35+
if (!fs.existsSync(DOWNSTREAM_CACHE)) {
3236
console.log('making .downstream_cache working directory');
33-
fs.mkdirSync(DOWNSTREAMS_PATH);
37+
fs.mkdirSync(DOWNSTREAM_CACHE);
3438
}
3539
}
3640

@@ -40,15 +44,6 @@ function localPublish() {
4044
util._exec('yalc publish');
4145
}
4246

43-
function initializeDownstreams() {
44-
Object.keys(config).forEach(key => {
45-
const installTargetDir = path.resolve(DOWNSTREAMS_PATH, key);
46-
const installSource = config[key];
47-
const flags = { noBuild: true, noPublish: true };
48-
publishYalcPackage(installTargetDir, installSource, flags);
49-
});
50-
}
51-
5247
function installUpstreamDeps() {
5348
UPSTREAM_PKGS.forEach(upstream => util._exec('yalc add ' + upstream));
5449
}
@@ -64,27 +59,56 @@ function runTests() {
6459
}
6560
}
6661

67-
function revertLocalChanges(key, source) {
62+
function revertLocalChanges(source) {
6863
const isRemoteSource = source[0] !== '.';
6964
const ref = isRemoteSource ? 'origin/master' : 'master';
7065
util._exec(`git reset --hard ${ref}`);
7166
util._exec('git clean --force -d');
7267
}
7368

74-
console.log(` ===> Making working copy <===`);
75-
makeWorkingCopy();
69+
try {
70+
console.log(` ===> Making .downstream_cache working directory <===`);
71+
makeDownstreamCache();
7672

77-
console.log(` ===> Publishing ${pkgjson.name} to yalc registry <===`);
78-
localPublish();
73+
console.log(` ===> Publishing ${pkgjson.name} to yalc registry <===`);
74+
localPublish();
7975

80-
console.log(` ===> Fetching downstream projects and their dependencies <===`);
81-
initializeDownstreams();
76+
Object.keys(config).forEach(key => {
77+
if (DOWNSTREAM_PKGS.length && DOWNSTREAM_PKGS.indexOf(key) === -1) {
78+
console.log(callback.constructor.name + ": " + key + ' not in DOWNSTREAM_PKGS, skipping...');
79+
return;
80+
}
8281

83-
console.log(` ===> Installing freshly built upstream packages <===`);
84-
forEachDownstream(installUpstreamDeps);
82+
const DOWNSTREAM_PACKAGE_DIR = path.resolve(DOWNSTREAM_CACHE, key);
83+
process.chdir(PKG_DIR);
84+
85+
console.log(` ===> Fetching downstream project '${key}' and its dependencies <===`);
86+
const installTargetDir = DOWNSTREAM_PACKAGE_DIR;
87+
const installSource = config[key];
88+
const flags = { noBuild: true, noPublish: true };
89+
publishYalcPackage(installTargetDir, installSource, flags);
8590

86-
console.log(` ===> Running downstream tests <===`);
87-
forEachDownstream(runTests);
91+
console.log(` ===> Installing freshly built upstream packages <===`);
92+
process.chdir(DOWNSTREAM_PACKAGE_DIR);
93+
installUpstreamDeps();
94+
95+
const DOWNSTREAM_PACKAGE_TEMP_DIR = path.resolve(TEMP_DIR, path.basename(DOWNSTREAM_PACKAGE_DIR));
96+
try {
97+
console.log(` ===> Moving downstream project '${key}' to temp dir '${TEMP_DIR}' <===`);
98+
shelljs.mv(DOWNSTREAM_PACKAGE_DIR, TEMP_DIR);
99+
100+
console.log(` ===> Running downstream tests <===`);
101+
process.chdir(DOWNSTREAM_PACKAGE_TEMP_DIR);
102+
runTests();
103+
} finally {
104+
console.log(` ===> Moving downstream project '${key}' back from temp dir <===`);
105+
shelljs.mv(DOWNSTREAM_PACKAGE_TEMP_DIR, DOWNSTREAM_CACHE);
106+
}
88107

89-
console.log(` ===> Cleaning downstream projects <===`);
90-
forEachDownstream(revertLocalChanges);
108+
console.log(` ===> Cleaning downstream project '${key}' <===`);
109+
process.chdir(DOWNSTREAM_PACKAGE_DIR);
110+
revertLocalChanges(installSource);
111+
});
112+
} finally {
113+
shelljs.rm('-rf', TEMP_DIR)
114+
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,10 @@ os-locale@^2.0.0:
841841
lcid "^1.0.0"
842842
mem "^1.1.0"
843843

844+
os-tmpdir@~1.0.2:
845+
version "1.0.2"
846+
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
847+
844848
p-finally@^1.0.0:
845849
version "1.0.0"
846850
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"
@@ -1222,6 +1226,12 @@ through@2, "through@>=2.2.7 <3", through@~2.3, through@~2.3.1:
12221226
version "2.3.8"
12231227
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
12241228

1229+
tmp@^0.0.33:
1230+
version "0.0.33"
1231+
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
1232+
dependencies:
1233+
os-tmpdir "~1.0.2"
1234+
12251235
trim-newlines@^1.0.0:
12261236
version "1.0.0"
12271237
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"

0 commit comments

Comments
 (0)