/
index-test.js
165 lines (133 loc) · 5.3 KB
/
index-test.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
'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const execa = require('execa');
const createServer = require('./helpers/server').createServer;
const getChannelURL = require('../src');
const tmp = require('tmp');
tmp.setGracefulCleanup();
const ROOT = process.cwd();
const EXECUTABLE_PATH = path.join(__dirname, '..', 'bin', 'ember-source-channel-url');
QUnit.module('ember-source-channel-url', function(hooks) {
function randomString(length) {
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex');
}
hooks.beforeEach(function() {
let dir = tmp.dirSync();
process.chdir(dir.name);
return createServer().then(server => {
process.env.EMBER_SOURCE_CHANNEL_URL_HOST = `http://localhost:${server.port}`;
this.server = server;
this.fakeSHA = randomString(20);
let assetPath = (this.assetPath = `/canary/shas/${this.fakeSHA}.tgz`);
this.expectedURL = `http://${server.host}:${server.port}/builds.emberjs.com${this.assetPath}`;
server.on('/builds.emberjs.com/canary.json', (req, res) => {
res.end(JSON.stringify({ assetPath }));
});
return server.listen(server.port);
});
});
hooks.afterEach(function() {
process.chdir(ROOT);
return this.server.close();
});
QUnit.test('works', function(assert) {
let expected = `http://${this.server.host}:${this.server.port}/builds.emberjs.com${
this.assetPath
}`;
return getChannelURL('canary').then(actual => {
assert.equal(actual, expected);
});
});
QUnit.module('binary', function() {
QUnit.test('works', function(assert) {
return execa(EXECUTABLE_PATH, ['canary']).then(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
});
});
QUnit.test('updates local package.json when -w is passed (dependencies)', function(assert) {
fs.writeFileSync(
'package.json',
JSON.stringify({ dependencies: { 'ember-source': '^3.10.0' } }),
{ encoding: 'utf8' }
);
return execa(EXECUTABLE_PATH, ['canary', '-w']).then(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), {
dependencies: {
'ember-source': this.expectedURL,
},
});
});
});
QUnit.test('updates local package.json when --write is passed (dependencies)', function(
assert
) {
fs.writeFileSync(
'package.json',
JSON.stringify({ dependencies: { 'ember-source': '^3.10.0' } }),
{ encoding: 'utf8' }
);
return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), {
dependencies: {
'ember-source': this.expectedURL,
},
});
});
});
QUnit.test('updates local package.json when --write is passed (devDependencies)', function(
assert
) {
fs.writeFileSync(
'package.json',
JSON.stringify({ devDependencies: { 'ember-source': '^3.10.0' } }),
{ encoding: 'utf8' }
);
return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
assert.deepEqual(JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })), {
devDependencies: {
'ember-source': this.expectedURL,
},
});
});
});
QUnit.test('preserves line ending when updating package.json', function(assert) {
fs.writeFileSync(
'package.json',
JSON.stringify({ dependencies: { 'ember-source': '^3.10.0' } }, null, 2) + '\n',
{ encoding: 'utf8' }
);
return execa(EXECUTABLE_PATH, ['canary', '--write']).then(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
let expected =
JSON.stringify({ dependencies: { 'ember-source': this.expectedURL } }, null, 2) + '\n';
assert.deepEqual(fs.readFileSync('package.json', { encoding: 'utf8' }), expected);
});
});
QUnit.test('fails when package.json is missing', function(assert) {
return execa(EXECUTABLE_PATH, ['canary', '--write']).catch(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
assert.ok(
results.stdout.includes('no package.json is available to update'),
'warning is printed indicating -w failed'
);
});
});
QUnit.test('fails when ember-source is not a dep', function(assert) {
fs.writeFileSync('package.json', JSON.stringify({}), { encoding: 'utf8' });
return execa(EXECUTABLE_PATH, ['canary', '--write']).catch(results => {
assert.ok(results.stdout.includes(this.expectedURL), 'URL is present in stdout');
assert.ok(
results.stdout.includes(
'ember-source is not included in dependencies or devDependencies'
),
'warning is printed indicating -w failed'
);
});
});
});
});