Skip to content

Commit 08c46be

Browse files
committed
seed-tests
1 parent 33e51c0 commit 08c46be

File tree

5 files changed

+303
-0
lines changed

5 files changed

+303
-0
lines changed

seed-tests/jasmine.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"spec_dir": ".",
3+
"spec_files": ["./*tests.js"],
4+
"stopSpecOnExpectationFailure": false,
5+
"random": false
6+
}

seed-tests/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"devDependencies": {
3+
"async": "^2.4.1",
4+
"cross-env": "^5.1.3",
5+
"glob": "^7.1.2",
6+
"jasmine": "^2.6.0",
7+
"ncp": "^2.0.0",
8+
"rimraf": "^2.6.1"
9+
},
10+
"scripts": {
11+
"test.android": "cross-env ANDROID=true jasmine --config=jasmine.config.json",
12+
"test.ios": "cross-env IOS=true jasmine --config=jasmine.config.json"
13+
},
14+
"dependencies": {}
15+
}

seed-tests/postclone.tests.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
var exec = require('child_process').exec;
2+
var fs = require('fs');
3+
var glob = require("glob");
4+
var testUtils = require("./tests.utils");
5+
var constants = require("./tests.constants");
6+
7+
var _srcReadmeContent = "";
8+
9+
describe('postclone', function () {
10+
11+
// keep before 'should not init new git repo'
12+
// in order to avoid getting new files in the git repo
13+
it('should init new git repo', function (done) {
14+
testUtils.copySeedDir(constants.SEED_LOCATION, constants.SEED_COPY_LOCATION, function (err) {
15+
if (err) {
16+
done.fail(err);
17+
}
18+
19+
testUtils.callPostclone(constants.SEED_COPY_LOCATION, constants.TEST_GITHUB_USERNAME, constants.TEST_PLUGIN_NAME, "y", function (error) {
20+
if (error) {
21+
done.fail(error);
22+
} else {
23+
exec("cd " + constants.SEED_COPY_LOCATION + "/src && git config --get remote.origin.url", function (error, stdout, stderr) {
24+
expect(stdout).toEqual("");
25+
done();
26+
});
27+
}
28+
});
29+
});
30+
});
31+
32+
it('should not init new git repo', function (done) {
33+
testUtils.copySeedDir(constants.SEED_LOCATION, constants.SEED_COPY_LOCATION, function (err) {
34+
if (err) {
35+
done.fail(err);
36+
}
37+
38+
_srcReadmeContent = fs.readFileSync(constants.SEED_LOCATION + "/src/README.md");
39+
testUtils.callPostclone(constants.SEED_COPY_LOCATION, constants.TEST_GITHUB_USERNAME, constants.TEST_PLUGIN_NAME, "n", function (error, stdout) {
40+
if (error) {
41+
done.fail(error);
42+
} else {
43+
exec("cd " + constants.SEED_COPY_LOCATION + " && git config --get remote.origin.url", function (execError, stdout, stderr) {
44+
expect(stdout).toContain("NativeScript/nativescript-plugin-seed.git");
45+
done();
46+
});
47+
}
48+
});
49+
});
50+
});
51+
52+
it('should delete the seed screenshots folder', function () {
53+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/screenshots")).toBeFalsy();
54+
});
55+
56+
it('should delete the seed CONTRIBUTING.md', function () {
57+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/CONTRIBUTING.md")).toBeFalsy();
58+
});
59+
60+
it('should delete the seed CODE_OF_CONDUCT.md', function () {
61+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/CODE_OF_CONDUCT.md")).toBeFalsy();
62+
});
63+
64+
it('should delete the postclone.js', function () {
65+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/src/scripts/postclone.js")).toBeFalsy();
66+
});
67+
68+
it('should delete the seed tests folder', function () {
69+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/seed-tests")).toBeFalsy();
70+
});
71+
72+
it('should replace the seed README with the plugin one', function () {
73+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/README.md")).toBeTruthy();
74+
expect(fs.existsSync(constants.SEED_COPY_LOCATION + "/src/README.md")).toBeFalsy();
75+
76+
var readmeContent = fs.readFileSync(constants.SEED_COPY_LOCATION + "/README.md");
77+
expect(_srcReadmeContent).toEqual(readmeContent);
78+
});
79+
80+
it('should rename each yourplugin file', function (done) {
81+
glob(constants.SEED_COPY_LOCATION + "/**/yourplugin*.*", function (er, files) {
82+
expect(files.length).toEqual(0);
83+
done();
84+
});
85+
});
86+
87+
it('should rename each yourplugin file with the new plugin name', function (done) {
88+
glob(constants.SEED_COPY_LOCATION + "/**/" + constants.TEST_PLUGIN_NAME + "*.*", function (er, files) {
89+
expect(files.length).toBeGreaterThan(0);
90+
done();
91+
});
92+
});
93+
94+
it('should replace each yourplugin string', function (done) {
95+
testUtils.findInFiles("yourplugin", constants.SEED_COPY_LOCATION, function (resultsCount) {
96+
expect(resultsCount).toEqual(0);
97+
done();
98+
});
99+
});
100+
101+
it('should replace each YourPlugin string', function (done) {
102+
testUtils.findInFiles("YourPlugin", constants.SEED_COPY_LOCATION, function (resultsCount) {
103+
expect(resultsCount).toEqual(0);
104+
done();
105+
});
106+
});
107+
108+
it('should replace each yourPlugin string', function (done) {
109+
testUtils.findInFiles("yourPlugin", constants.SEED_COPY_LOCATION, function (resultsCount) {
110+
expect(resultsCount).toEqual(0);
111+
done();
112+
});
113+
});
114+
115+
it('should replace each YourName string', function (done) {
116+
testUtils.findInFiles("YourName", constants.SEED_COPY_LOCATION, function (resultsCount) {
117+
expect(resultsCount).toEqual(0);
118+
done();
119+
});
120+
});
121+
122+
it('should replace each YourName string with the test github username', function (done) {
123+
testUtils.findInFiles(constants.TEST_GITHUB_USERNAME, constants.SEED_COPY_LOCATION, function (resultsCount) {
124+
// plugin author in the package json
125+
expect(resultsCount).toEqual(1);
126+
done();
127+
});
128+
});
129+
130+
it('should replace each YourPlugin string with ThePlugin', function (done) {
131+
testUtils.findInFiles(constants.TEST_PLUGIN_NAME, constants.SEED_COPY_LOCATION, function (resultsCount) {
132+
expect(resultsCount).toBeGreaterThan(0);
133+
done();
134+
});
135+
});
136+
137+
it('should replace each nativescript-YourPlugin string with nativescript-ThePlugin', function (done) {
138+
testUtils.findInFiles("nativescript-" + constants.TEST_PLUGIN_NAME, constants.SEED_COPY_LOCATION, function (resultsCount) {
139+
expect(resultsCount).toBeGreaterThan(0);
140+
done();
141+
});
142+
});
143+
144+
it('should prepare a working demo with passing tests', function (done) {
145+
var testsCommand = "cd " + constants.SEED_COPY_LOCATION + "/src && npm run test";
146+
testsCommand += testUtils.isAndroid() ? ".android" : ".ios";
147+
exec(testsCommand, function (error, stdout, stderr) {
148+
expect(error).toBeNull();
149+
done();
150+
});
151+
});
152+
});

seed-tests/tests.constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
exports.SEED_LOCATION = "../";
2+
exports.SEED_COPY_LOCATION = "seed-copy";
3+
exports.DEFAULT_PLUGIN_NAME = "nativescript-yourplugin";
4+
exports.TEST_PLUGIN_NAME = "ThePlugin";
5+
exports.TEST_GITHUB_USERNAME = "TheGitHubUser";
6+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1200000; // 20 mins

seed-tests/tests.utils.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var exec = require('child_process').exec;
2+
var rimraf = require('rimraf');
3+
var ncp = require('ncp').ncp;
4+
var fs = require('fs');
5+
var async = require("async");
6+
var os = require('os');
7+
var constants = require('./tests.constants');
8+
9+
exports.findInFiles = function findInFiles(string, dir, callback) {
10+
var _resultsCount = 0;
11+
var _excludedPaths = ["node_modules", "src/scripts/postclone", "/seed-tests/", ".git"];
12+
13+
function _findInFiles(string, dir, callback) {
14+
fs.readdir(dir, function (err, entries) {
15+
entries = entries.filter(function (entry) {
16+
var fullEntry = dir + '/' + entry;
17+
var shouldBeIncluded = true;
18+
_excludedPaths.forEach(function callback(currentValue) {
19+
shouldBeIncluded = fullEntry.indexOf(currentValue) === -1 && shouldBeIncluded;
20+
});
21+
22+
return shouldBeIncluded;
23+
});
24+
async.eachSeries(entries, function (entry, foreachCallback) {
25+
entry = dir + '/' + entry;
26+
fs.stat(entry, function (err, stat) {
27+
if (stat && stat.isDirectory()) {
28+
_findInFiles(string, entry, foreachCallback);
29+
} else {
30+
fs.readFile(entry, 'utf-8', function (err, contents) {
31+
if (contents.indexOf(string) > -1) {
32+
_resultsCount++;
33+
}
34+
35+
foreachCallback();
36+
});
37+
}
38+
});
39+
}, function (err) {
40+
callback();
41+
});
42+
});
43+
};
44+
45+
_findInFiles(string, dir, function () {
46+
callback(_resultsCount);
47+
});
48+
};
49+
50+
exports.copySeedDir = function copySeedDir(seedLocation, copyLocation, callback) {
51+
rimraf.sync(copyLocation);
52+
53+
ncp(seedLocation, copyLocation, {
54+
filter: function (fileName) {
55+
if ((fileName.indexOf("seed-tests") > -1 && fileName.indexOf(constants.SEED_COPY_LOCATION) > -1) ||
56+
(fileName.indexOf("demo") > -1 && fileName.indexOf("node_modules") > -1)||
57+
(fileName.indexOf("seed-tests") > -1 && fileName.indexOf("node_modules") > -1) ||
58+
(fileName.indexOf("src") > -1 && fileName.indexOf("node_modules") > -1) ||
59+
(fileName.indexOf("demo") > -1 && fileName.indexOf("platforms") > -1)) {
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
}, function (err) {
66+
if (!err) {
67+
console.log(copyLocation + ' folder successfully created.');
68+
}
69+
callback(err);
70+
});
71+
};
72+
73+
exports.callPostclone = function callPostclone(seedLocation, githubUsername, pluginName, initGit, callback) {
74+
var postcloneScript = getPackageJsonPostcloneScript();
75+
postcloneScript = postcloneScript.replace("postclone.js", "postclone.js gitHubUsername=" + githubUsername + " pluginName=" + pluginName + " initGit=" + initGit);
76+
exec("cd " + seedLocation + "/src && " + postcloneScript, function (error, stdout, stderr) {
77+
callback(error, stdout, stderr);
78+
});
79+
};
80+
81+
exports.callDevelopmentSetup = function callDevelopmentSetup(seedLocation, callback) {
82+
exec("cd " + seedLocation + "/src && npm run development.setup", function (error, stdout, stderr) {
83+
callback(error, stdout, stderr);
84+
});
85+
};
86+
87+
exports.getNpmLinks = function getNpmLinks(callback) {
88+
exec("npm list -g --depth=0", function (error, stdout, stderr) {
89+
var links = stdout.split(os.EOL)
90+
.map(function (item) {
91+
return item.replace("├──", "").replace("└──", "").trim();
92+
})
93+
.filter(function (item) {
94+
return !!item && item.indexOf("->") !== -1;
95+
});
96+
97+
callback(links);
98+
});
99+
}
100+
101+
exports.removeNpmLink = function removeNpmLink(packageName, callback) {
102+
exec("npm remove " + packageName + " -g", function (error, stdout, stderr) {
103+
callback();
104+
});
105+
}
106+
107+
exports.isAndroid = function isAndroid() {
108+
return !!!process.env.IOS;
109+
}
110+
111+
function getPackageJsonPostcloneScript() {
112+
var packageJsonFile = constants.SEED_COPY_LOCATION + "/src/package.json";
113+
114+
if (fs.lstatSync(packageJsonFile).isFile()) {
115+
var packageJson = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8'));
116+
var packageJsonScripts = packageJson["scripts"];
117+
118+
if (packageJsonScripts && packageJsonScripts["postclone"]) {
119+
return packageJsonScripts["postclone"];
120+
};
121+
}
122+
123+
return "";
124+
}

0 commit comments

Comments
 (0)