forked from aws/aws-cdk-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.test.ts
More file actions
155 lines (125 loc) · 5.36 KB
/
version.test.ts
File metadata and controls
155 lines (125 loc) · 5.36 KB
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
/* eslint-disable import/order */
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import { setTimeout as _setTimeout } from 'timers';
import { promisify } from 'util';
import * as npm from '../../lib/cli/util/npm';
import { displayVersionMessage, getVersionMessages, isDeveloperBuild, VersionCheckTTL } from '../../lib/cli/version';
import * as logging from '../../lib/logging';
jest.setTimeout(10_000);
const setTimeout = promisify(_setTimeout);
function tmpfile(): string {
return `/tmp/version-${Math.floor(Math.random() * 10000)}`;
}
beforeEach(() => {
process.chdir(os.tmpdir()); // Need a chdir because in the workspace 'npm view' will take a long time
});
afterEach(done => {
sinon.restore();
done();
});
test('initialization fails on unwritable directory', () => {
const cacheFile = tmpfile();
sinon.stub(fs, 'mkdirsSync').withArgs(path.dirname(cacheFile)).throws('Cannot make directory');
expect(() => new VersionCheckTTL(cacheFile)).toThrow(/not writable/);
});
test('cache file responds correctly when file is not present', async () => {
const cache = new VersionCheckTTL(tmpfile(), 1);
expect(await cache.hasExpired()).toBeTruthy();
});
test('cache file honours the specified TTL', async () => {
const cache = new VersionCheckTTL(tmpfile(), 1);
await cache.update();
expect(await cache.hasExpired()).toBeFalsy();
await setTimeout(1001); // Just above 1 sec in ms
expect(await cache.hasExpired()).toBeTruthy();
});
test('Skip version check if cache has not expired', async () => {
const cache = new VersionCheckTTL(tmpfile(), 100);
await cache.update();
expect(await getVersionMessages('0.0.0', cache)).toEqual([]);
});
test('Version specified is stored in the TTL file', async () => {
const cacheFile = tmpfile();
const cache = new VersionCheckTTL(cacheFile, 1);
await cache.update('1.1.1');
const storedVersion = fs.readFileSync(cacheFile, 'utf8');
expect(storedVersion).toBe('1.1.1');
});
test('No Version specified for storage in the TTL file', async () => {
const cacheFile = tmpfile();
const cache = new VersionCheckTTL(cacheFile, 1);
await cache.update();
const storedVersion = fs.readFileSync(cacheFile, 'utf8');
expect(storedVersion).toBe('');
});
test('Skip version check if environment variable is set', async () => {
sinon.stub(process, 'stdout').value({ ...process.stdout, isTTY: true });
sinon.stub(process, 'env').value({ ...process.env, CDK_DISABLE_VERSION_CHECK: '1' });
const printStub = sinon.stub(logging, 'info');
await displayVersionMessage();
expect(printStub.called).toEqual(false);
});
describe('version message', () => {
const previousIsTty = process.stdout.isTTY;
beforeAll(() => {
process.stdout.isTTY = true;
});
afterAll(() => {
process.stdout.isTTY = previousIsTty;
});
test('Prints messages when a new version is available', async () => {
const mockCache = new VersionCheckTTL(tmpfile());
jest.spyOn(mockCache, 'hasExpired').mockResolvedValue(true);
jest.spyOn(npm, 'execNpmView').mockResolvedValue({
latestVersion: '2.0.0',
deprecated: undefined,
});
const messages = await getVersionMessages('1.0.0', mockCache);
expect(messages.some(msg => msg.includes('Newer version of CDK is available'))).toBeTruthy();
expect(messages.some(msg => msg.includes('Information about upgrading from version 1.x to version 2.x'))).toBeTruthy();
expect(messages.some(msg => msg.includes('Upgrade recommended (npm install -g aws-cdk)'))).toBeTruthy();
})
test('Does not include major upgrade documentation when unavailable', async () => {
const mockCache = new VersionCheckTTL(tmpfile());
jest.spyOn(mockCache, 'hasExpired').mockResolvedValue(true);
jest.spyOn(npm, 'execNpmView').mockResolvedValue({
latestVersion: '2.1000.0',
deprecated: undefined,
});
const messages = await getVersionMessages('2.179.0', mockCache);
const hasUpgradeDoc = messages.some(msg =>
msg.includes('Information about upgrading from version 1.x to version 2.x')
);
expect(hasUpgradeDoc).toBeFalsy();
})
test('Prints a message when a deprecated version is used', async () => {
const mockCache = new VersionCheckTTL(tmpfile());
jest.spyOn(mockCache, 'hasExpired').mockResolvedValue(true);
jest.spyOn(npm, 'execNpmView').mockResolvedValue({
latestVersion: '2.0.0',
deprecated: 'This version is deprecated.',
});
const messages = await getVersionMessages('1.0.0', mockCache);
expect(messages.some(msg => msg.includes('This version is deprecated'))).toBeTruthy();
})
test('Does not print message when current version is up to date', async () => {
const mockCache = new VersionCheckTTL(tmpfile());
jest.spyOn(mockCache, 'hasExpired').mockResolvedValue(true);
jest.spyOn(npm, 'execNpmView').mockResolvedValue({
latestVersion: '1.0.0',
deprecated: undefined,
});
const messages = await getVersionMessages('1.0.0', mockCache);
expect(messages).toEqual([]);
})
});
test('isDeveloperBuild call does not throw an error', () => {
// To be frank: this is just to shut CodeCov up. It don't want to make an assertion
// that the value is `true` when running tests, because I won't want to make too
// many assumptions for no good reason.
isDeveloperBuild();
// THEN: should not explode
});