This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
/
version-service.ts
195 lines (169 loc) · 6.63 KB
/
version-service.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { VersionService } from "../../lib/services/version-service";
import { PolyfillService } from "../../lib/services/polyfill-service";
import { Yok } from "nativescript/lib/common/yok";
import { assert } from "chai";
describe("versionService", () => {
const createTestInjector = (cloudConfigurationData?: ICloudConfigurationData): IInjector => {
const testInjector = new Yok();
testInjector.register("logger", {
trace: (formatStr?: any, ...args: any[]): void => (undefined)
});
testInjector.register("httpClient", {});
testInjector.register("nsCloudConfigurationService", {
getCloudConfigurationData: () => cloudConfigurationData
});
testInjector.register("nsCloudPolyfillService", PolyfillService);
testInjector.register("projectDataService", {});
return testInjector;
};
let isHttpRequestCalled = false;
beforeEach(() => {
isHttpRequestCalled = false;
});
describe("getCliVersion", () => {
const cliVersion = "5.0.0";
it("returns process.env.TNS_CLI_CLOUD_VERSION when it is set", async () => {
const testInjector = createTestInjector({
tnsCliCloudVersion: cliVersion
});
const versionService = testInjector.resolve<IVersionService>(VersionService);
const actualCliVersion = await versionService.getCliVersion(null);
assert.equal(actualCliVersion, cliVersion);
});
const testData: any[] = [
{
runtimeVersion: "5.0.0",
expectedCliVersion: "5.0.2"
},
{
runtimeVersion: "5.0.2-1",
expectedCliVersion: "5.0.2"
},
{
runtimeVersion: "5.0.3-1",
expectedCliVersion: "5.0.2"
},
{
runtimeVersion: "5.0.2",
expectedCliVersion: "5.0.2"
},
{
runtimeVersion: "5.0.5",
expectedCliVersion: "5.0.2"
},
{
runtimeVersion: "5.1.5",
expectedCliVersion: "5.1.0"
},
{
runtimeVersion: "7.0.0-0",
expectedCliVersion: "7.0.0-2"
},
{
runtimeVersion: "7.0.1-1",
expectedCliVersion: "7.0.1-2"
},
{
runtimeVersion: "7.0.1-5",
expectedCliVersion: "7.0.1-2"
},
{
runtimeVersion: "7.0.2-5",
expectedCliVersion: "7.0.0-2"
}
];
const mockHttpRequest = (testInjector: IInjector, body: IDictionary<any>, error?: Error) => {
const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient");
httpClient.httpRequest = async (url: string): Promise<any> => {
isHttpRequestCalled = true;
assert.equal(url, "http://registry.npmjs.org/nativescript");
if (error) {
throw error;
}
return { body: JSON.stringify(body) };
};
};
_.each(testData, ({ runtimeVersion, expectedCliVersion }) => {
it(`returns correct CLI version, based on runtime version, input: ${runtimeVersion}, expectedOutput: ${expectedCliVersion}`, async () => {
const testInjector = createTestInjector();
const body = {
versions: {
"1.0.0": {},
"5.0.0": {},
"5.0.1": {},
"5.0.2": {},
"5.0.2-1": {},
"5.1.0": {},
"6.0.0": {},
"7.0.0-1": {},
"7.0.0-2": {},
"7.0.1-1": {},
"7.0.1-2": {},
}
};
mockHttpRequest(testInjector, body);
const versionService = testInjector.resolve<IVersionService>(VersionService);
const actualCliVersion = await versionService.getCliVersion(runtimeVersion);
assert.equal(actualCliVersion, expectedCliVersion);
assert.isTrue(isHttpRequestCalled, "An http request to registry.npmjs.org should have been made in order to get data for CLI.");
});
});
it("fails when unable to determine CLI version as runtime version is not valid semver version", async () => {
const testInjector = createTestInjector();
const body = {
versions: {
"5.0.0": {}
}
};
mockHttpRequest(testInjector, body);
const versionService = testInjector.resolve<IVersionService>(VersionService);
const invalidRuntimeVersion = "some invalid version";
await assert.isRejected(versionService.getCliVersion(invalidRuntimeVersion), `Unable to determine CLI version for cloud build based on project's runtime version: ${invalidRuntimeVersion}. Error is: Invalid Version: ${invalidRuntimeVersion}`);
assert.isFalse(isHttpRequestCalled, "An http request to registry.npmjs.org should have NOT been made.");
});
it("fails when http request to get CLI versions fails", async () => {
const testInjector = createTestInjector();
const err = new Error("Http request fails.");
mockHttpRequest(testInjector, null, err);
const versionService = testInjector.resolve<IVersionService>(VersionService);
const runtimeVersion = "1.2.3";
await assert.isRejected(versionService.getCliVersion(runtimeVersion), `Unable to determine CLI version for cloud build based on project's runtime version: ${runtimeVersion}. Error is: Cannot find CLI versions.`);
assert.isTrue(isHttpRequestCalled, "An http request to registry.npmjs.org should have been made in order to get data for CLI.");
});
});
describe("getProjectRuntimeVersion", () => {
let isGetNSValueCalled = false;
beforeEach(() => {
isGetNSValueCalled = false;
});
const mockGetNSValue = (testInjector: IInjector, runtimeVersion: string, projectDir: string, platform: string): void => {
const projectDataService = testInjector.resolve<IProjectDataService>("projectDataService");
projectDataService.getNSValue = (projDir: string, propertyName: string): any => {
isGetNSValueCalled = true;
assert.equal(projDir, projectDir, "getNSValue should be called with the projectDir passed to getProjectRuntimeVersion");
assert.equal(propertyName, `tns-${platform}.version`);
return runtimeVersion;
};
};
it("returns the version from package.json", async () => {
const testInjector = createTestInjector();
const projectDir = "project dir";
const platform = "platform";
const runtimeVersion = "3.0.0";
mockGetNSValue(testInjector, runtimeVersion, projectDir, platform);
const versionService = testInjector.resolve<IVersionService>(VersionService);
const actualRuntimeVersion = await versionService.getProjectRuntimeVersion(projectDir, platform);
assert.equal(actualRuntimeVersion, runtimeVersion);
assert.isTrue(isGetNSValueCalled, "getNSValue must be called");
});
it("fails when runtime is not added to package.json", async () => {
const testInjector = createTestInjector();
const projectDir = "project dir";
const platform = "platform";
mockGetNSValue(testInjector, null, projectDir, platform);
const versionService = testInjector.resolve<IVersionService>(VersionService);
await assert.isRejected(versionService.getProjectRuntimeVersion(projectDir, platform), `Unable to find runtime version for package tns-${platform}.`);
assert.isTrue(isGetNSValueCalled, "getNSValue must be called");
});
});
});