-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnpm-installation-manager.ts
180 lines (152 loc) · 6.46 KB
/
npm-installation-manager.ts
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
import {assert} from "chai";
import * as ConfigLib from "../lib/config";
import * as ErrorsLib from "../lib/common/errors";
import * as FsLib from "../lib/common/file-system";
import * as HostInfoLib from "../lib/common/host-info";
import * as LoggerLib from "../lib/common/logger";
import * as NpmInstallationManagerLib from "../lib/npm-installation-manager";
import * as OptionsLib from "../lib/options";
import * as StaticConfigLib from "../lib/config";
import Future = require("fibers/future");
import * as yok from "../lib/common/yok";
import ChildProcessLib = require("../lib/common/child-process");
function createTestInjector(): IInjector {
let testInjector = new yok.Yok();
testInjector.register("config", ConfigLib.Configuration);
testInjector.register("logger", LoggerLib.Logger);
testInjector.register("lockfile", { });
testInjector.register("errors", ErrorsLib.Errors);
testInjector.register("options", OptionsLib.Options);
testInjector.register("fs", FsLib.FileSystem);
testInjector.register("hostInfo", HostInfoLib.HostInfo);
testInjector.register("staticConfig", StaticConfigLib.StaticConfig);
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
testInjector.register("npmInstallationManager", NpmInstallationManagerLib.NpmInstallationManager);
return testInjector;
}
function mockNpm(testInjector: IInjector, versions: string[], latestVersion: string) {
testInjector.register("npm", {
view: (packageName: string, config: any) => {
return(() => {
if(config.versions) {
return versions;
}
throw new Error(`Unable to find propertyName ${config}.`);
}).future<any>()();
}
});
}
interface ITestData {
/**
* All versions of the package, including the ones from another tags.
*/
versions: string[];
/**
* The version under latest tag.
*/
packageLatestVersion: string;
/**
* Version of nativescript-cli, based on which the version of the package that will be installed is detected.
*/
cliVersion: string;
/**
* Expected result
*/
expectedResult: string;
}
describe("Npm installation manager tests", () => {
let testData: IDictionary<ITestData> = {
"when there's only one available version and it matches CLI's version": {
versions: ["1.4.0"],
packageLatestVersion: "1.4.0",
cliVersion: "1.4.0",
expectedResult: "1.4.0"
},
"when there's only one available version and it is higher than match CLI's version": {
versions: ["1.4.0"],
packageLatestVersion: "1.4.0",
cliVersion: "1.2.0",
expectedResult: "1.4.0"
},
"when there's only one available version and it is lower than CLI's version": {
versions: ["1.4.0"],
packageLatestVersion: "1.4.0",
cliVersion: "1.6.0",
expectedResult: "1.4.0"
},
"when there are multiple package versions and the latest one matches ~<cli-version>":{
versions: ["1.2.0", "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.4.0"],
packageLatestVersion: "1.3.3",
cliVersion: "1.3.0",
expectedResult: "1.3.3"
},
"when there are multiple package versions and the latest one matches ~<cli-version> when there are newer matching versions but they are not under latest tag":{
versions: ["1.2.0", "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.4.0"],
packageLatestVersion: "1.3.2",
cliVersion: "1.3.0",
expectedResult: "1.3.2"
},
"when there are multiple package versions and the latest one is lower than ~<cli-version>": {
versions: ["1.2.0", "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.4.0"],
packageLatestVersion: "1.4.0",
cliVersion: "1.5.0",
expectedResult: "1.4.0"
},
"when there are multiple package versions and there's beta version matching CLI's semver": {
versions: ["1.2.0", "1.3.0", "1.3.1", "1.4.0", "1.5.0-2016-02-25-182"],
packageLatestVersion: "1.4.0",
cliVersion: "1.5.0",
expectedResult: "1.4.0"
},
"when there are multiple package versions and package's latest version is greater than CLI's version": {
versions: ["1.2.0", "1.3.0", "1.3.1", "1.4.0", "1.5.0-2016-02-25-182", "1.5.0", "1.6.0"],
packageLatestVersion: "1.6.0",
cliVersion: "1.5.0",
expectedResult: "1.5.0"
},
"when there are multiple versions latest one does not match CLI's semver and other versions are not matching either": {
versions: ["1.0.0", "1.0.1", "1.2.0", "1.3.1", "1.4.0", "1.5.0-2016-02-25-182", "1.5.0"],
packageLatestVersion: "1.0.0",
cliVersion: "1.1.0",
expectedResult: "1.0.0"
},
"when CLI's version is beta (has dash) latest matching beta version is returned": {
versions: ["1.0.0", "1.0.1", "1.4.0", "1.5.0-2016-02-25-182", "1.5.0-2016-02-26-202"],
packageLatestVersion: "1.4.0",
cliVersion: "1.5.0-182",
expectedResult: "1.5.0-2016-02-26-202"
},
"when CLI's version is beta (has dash) latest matching official version is returned when beta versions do not match": {
versions: ["1.0.0", "1.0.1", "1.4.0", "1.5.0-2016-02-25-182", "1.5.0-2016-02-26-202"],
packageLatestVersion: "1.4.0",
cliVersion: "1.6.0-2016-03-01-182",
expectedResult: "1.4.0"
},
"when CLI's version is beta (has dash) latest matching official version is returned when beta versions do not match (when the prerelease of CLI is higher than prerelease version of runtime)": {
versions: ["1.0.0", "1.0.1", "1.4.0", "1.6.0-2016-02-25-182", "1.6.0-2016-02-26-202"],
packageLatestVersion: "1.4.0",
cliVersion: "1.6.0-2016-10-01-182",
expectedResult: "1.4.0"
},
"When CLI Version has patch version larger than an existing package, should return max compliant package from the same major.minor version": {
versions: ["1.0.0", "1.0.1", "1.4.0", "2.5.0", "2.5.1", "2.5.2"],
packageLatestVersion: "3.0.0",
cliVersion: "2.5.4",
expectedResult: "2.5.2"
}
};
_.each(testData, (currentTestData: ITestData, testName: string) => {
it(`returns correct latest compatible version, ${testName}`, () => {
let testInjector = createTestInjector();
mockNpm(testInjector, currentTestData.versions, currentTestData.packageLatestVersion);
// Mock staticConfig.version
let staticConfig = testInjector.resolve("staticConfig");
staticConfig.version = currentTestData.cliVersion;
// Mock npmInstallationManager.getLatestVersion
let npmInstallationManager = testInjector.resolve("npmInstallationManager");
npmInstallationManager.getLatestVersion = (packageName: string) => Future.fromResult(currentTestData.packageLatestVersion);
let actualLatestCompatibleVersion = npmInstallationManager.getLatestCompatibleVersion("").wait();
assert.equal(actualLatestCompatibleVersion, currentTestData.expectedResult);
});
});
});