-
Notifications
You must be signed in to change notification settings - Fork 7
/
test-project.js
182 lines (167 loc) · 6.3 KB
/
test-project.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright IBM Corp. 2014,2017. All Rights Reserved.
// Node module: strong-tools
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var fs = require('fs');
var helpers = require('./helpers');
var mkdirp = require('mkdirp');
var path = require('path');
var rimraf = require('rimraf');
var test = require('tap').test;
var Project = require('../lib/project');
var SANDBOX = path.resolve(__dirname, 'SANDBOX-project');
var SANDBOX_PKG = path.resolve(SANDBOX, 'package.json');
var SANDBOX_BOWER = path.resolve(SANDBOX, 'bower.json');
var SANDBOX_SHRINKWRAP = path.resolve(SANDBOX, 'npm-shrinkwrap.json');
var SANDBOX_PACKAGE_LOCK = path.resolve(SANDBOX, 'package-lock.json');
test('setup', function(t) {
helpers.resetSandboxSync(t, SANDBOX, SANDBOX_PKG, {
name: 'testing',
repository: 'git@github.com:myOrg/testing.git',
});
});
test('API', function(t) {
t.strictEqual(typeof require('../lib/project'), 'function',
'project exports a function');
t.ok(new Project('') instanceof Project,
'Project is a constructor');
t.ok(Project('') instanceof Project,
'does not require new');
t.ok(Project(SANDBOX).pkgJSONPath === SANDBOX_PKG,
'knows how to find package.json from directory');
t.ok(Project(SANDBOX_PKG).rootPath === SANDBOX,
'knows how to find package root from package.json');
t.end();
});
test('package parsing', function(t) {
var original = JSON.parse(fs.readFileSync(SANDBOX_PKG, 'utf8'));
var p1 = new Project(SANDBOX);
t.ok(!('version' in p1.rawPkgJSON),
'does not modify data on load');
t.strictEqual(p1.nameVer(), 'testing@1.0.0-0');
t.strictEqual(p1.license(), 'SEE LICENSE.md');
t.strictEqual(p1.version(), '1.0.0-0',
'reports 1.0.0-0 as version if missing');
p1.version(p1.version());
p1.persist();
var updated = JSON.parse(fs.readFileSync(SANDBOX_PKG, 'utf8'));
t.notEqual(updated, original, 'file has changed');
t.strictEqual(updated.version, '1.0.0-0',
'persists the updated version');
// test ghSlug generation BEFORE we've looked at git repo
t.equal(p1.ghSlug(), 'myOrg/testing');
t.end();
});
test('package info gathering with git', function(t) {
var pkgPath = require.resolve('../package.json');
var self = new Project(pkgPath, function(err, project) {
t.ifErr(err, 'should not error out');
t.same(self, project);
t.equal(project.get('license'), 'MIT');
t.equal(project.license(), 'MIT');
// fake out the name so we know it wasn't used to generate the gh slug
project.normalizedPkgJSON.name = 'not-really';
t.equal(project.ghSlug(), 'strongloop/strong-tools');
t.end();
});
});
test('package info gathering without git', function(t) {
var self = new Project(require.resolve('../package.json'));
self.git = null;
self.gather(function(err, project) {
t.ifErr(err, 'should not error out');
t.notOk(project.git, 'project has no git reference');
t.same(self, project);
t.equal(project.get('license'), 'MIT');
t.equal(project.license(), 'MIT');
t.end();
});
});
test('optionalDep getter/setter', function(t) {
var self = new Project(require.resolve('../package.json'));
self.gather(function(err, project) {
t.ifErr(err, 'should not error out');
t.same(self, project);
t.notOk(self.optionalDep('not-real'));
self.optionalDep('not-real', '1.0.0');
t.equal(self.optionalDep('not-real'), '1.0.0');
self.optionalDep('not-real', null);
t.notOk(self.optionalDep('not-real'));
t.end();
});
});
test('script getter/setter', function(t) {
var self = new Project(require.resolve('../package.json'));
self.gather(function(err, project) {
t.ifErr(err, 'should not error out');
t.same(self, project);
t.notOk(self.script('not-real'));
self.script('not-real', 'foo');
t.equal(self.script('not-real'), 'foo');
self.script('not-real', null);
t.notOk(self.script('not-real'));
t.end();
});
});
test('github repo slug extraction', function(t) {
var testCases = [
['https://github.com/myOrg/myRepo', 'myOrg/myRepo'],
['https://github.com/myOrg/myRepo.git', 'myOrg/myRepo'],
['git@github.com:myOrg/myRepo', 'myOrg/myRepo'],
['git@github.com:myOrg/myRepo.git', 'myOrg/myRepo'],
['git+ssh://git@github.com/myOrg/myRepo', 'myOrg/myRepo'],
['git+ssh://git@github.com/myOrg/myRepo.git', 'myOrg/myRepo'],
['https://github.corp.com/myOrg/myRepo', undefined],
['https://bitbucket.org/myOrg/myRepo', undefined],
];
t.plan(testCases.length);
testCases.forEach(function(tc) {
t.equal(Project.ghSlugFrom(tc[0]), tc[1]);
});
});
test('bower support', function(t) {
var pkgjson = JSON.parse(fs.readFileSync(SANDBOX_PKG, 'utf8'));
fs.writeFileSync(SANDBOX_BOWER, JSON.stringify(pkgjson), 'utf8');
var p1 = new Project(SANDBOX);
t.strictEqual(p1.version(), '1.0.0-0');
p1.version('2.3.4');
p1.persist();
var bower = JSON.parse(fs.readFileSync(SANDBOX_BOWER, 'utf8'));
t.strictEqual(p1.version(), '2.3.4');
t.strictEqual(bower.version, '2.3.4');
t.end();
});
test('package-lock support', function(t) {
var pkgjson = JSON.parse(fs.readFileSync(SANDBOX_PKG, 'utf8'));
fs.writeFileSync(SANDBOX_PACKAGE_LOCK, JSON.stringify(pkgjson), 'utf8');
var p1 = new Project(SANDBOX);
t.strictEqual(p1.version(), '2.3.4');
p1.version('4.5.6');
p1.persist();
var pkgLock = JSON.parse(fs.readFileSync(SANDBOX_PACKAGE_LOCK, 'utf8'));
t.strictEqual(p1.version(), '4.5.6');
t.strictEqual(pkgLock.version, '4.5.6');
t.end();
});
test('shrinkwrap support', function(t) {
var pkgjson = JSON.parse(fs.readFileSync(SANDBOX_PKG, 'utf8'));
fs.writeFileSync(SANDBOX_SHRINKWRAP, JSON.stringify(pkgjson), 'utf8');
var p1 = new Project(SANDBOX);
t.strictEqual(p1.version(), '4.5.6');
p1.version('3.4.5');
p1.persist();
var shrinkwrap = JSON.parse(fs.readFileSync(SANDBOX_SHRINKWRAP, 'utf8'));
t.strictEqual(p1.version(), '3.4.5');
t.strictEqual(shrinkwrap.version, '3.4.5');
t.end();
});
test('package inference without package.json', function(t) {
var SANDBOX_NOJSON = path.resolve(__dirname, 'SANDBOX-no-json');
rimraf.sync(SANDBOX_NOJSON);
mkdirp.sync(SANDBOX_NOJSON);
var p1 = new Project(SANDBOX_NOJSON);
t.equal(p1.nameVer(), 'SANDBOX-no-json@1.0.0-0');
t.equal(p1.license(), 'SEE LICENSE.md');
t.end();
});