-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-changes-service.ts
209 lines (179 loc) · 7.78 KB
/
project-changes-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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as path from "path";
import { BaseServiceTest } from "./base-service-test";
import temp = require("temp");
import { assert } from "chai";
import { PlatformsDataService } from "../lib/services/platforms-data-service";
import { ProjectChangesService } from "../lib/services/project-changes-service";
import * as Constants from "../lib/constants";
import { FileSystem } from "../lib/common/file-system";
import { HooksServiceStub, LoggerStub } from "./stubs";
// start tracking temporary folders/files
temp.track();
class ProjectChangesServiceTest extends BaseServiceTest {
public projectDir: string;
constructor() {
super();
}
initInjector(): void {
this.projectDir = temp.mkdirSync("projectDir");
this.injector.register("projectData", {
projectDir: this.projectDir
});
this.injector.register("platformsDataService", PlatformsDataService);
this.injector.register("androidProjectService", {});
this.injector.register("iOSProjectService", {});
this.injector.register("fs", FileSystem);
this.injector.register("devicePlatformsConstants", {});
this.injector.register("devicePlatformsConstants", {});
this.injector.register("projectChangesService", ProjectChangesService);
this.injector.register("filesHashService", {
generateHashes: () => Promise.resolve({})
});
this.injector.register("logger", LoggerStub);
this.injector.register("hooksService", HooksServiceStub);
this.injector.register("nodeModulesDependenciesBuilder", {});
const fs = this.injector.resolve<IFileSystem>("fs");
fs.writeJson(path.join(this.projectDir, Constants.PACKAGE_JSON_FILE_NAME), {
nativescript: {
id: "org.nativescript.test"
}
});
}
get projectChangesService(): IProjectChangesService {
return this.injector.resolve("projectChangesService");
}
get projectData(): IProjectData {
return this.injector.resolve("projectData");
}
get getNativeProjectDataService(): any {
return this.injector.resolve("platformsDataService");
}
getPlatformData(platform: string): IPlatformData {
return <any>{
projectRoot: path.join(this.projectDir, "platforms", platform.toLowerCase()),
platformProjectService: {
checkForChanges: async (changesInfo: IProjectChangesInfo) => {
changesInfo.signingChanged = true;
}
}
};
}
}
describe("Project Changes Service Tests", () => {
let serviceTest: ProjectChangesServiceTest;
beforeEach(() => {
serviceTest = new ProjectChangesServiceTest();
const platformsDir = path.join(
serviceTest.projectDir,
Constants.PLATFORMS_DIR_NAME
);
serviceTest.getNativeProjectDataService.getPlatformData =
(platform: string) => {
if (platform.toLowerCase() === "ios") {
return {
projectRoot: path.join(platformsDir, platform),
get platformProjectService(): any {
return {
checkForChanges(changesInfo: IProjectChangesInfo): void {
changesInfo.signingChanged = true;
}
};
}
};
} else {
return {
projectRoot: path.join(platformsDir, platform),
get platformProjectService(): any {
return {
checkForChanges(changesInfo: IProjectChangesInfo): void { /* no android changes */ }
};
}
};
}
};
});
describe("Get Prepare Info File Path", () => {
it("Gets the correct Prepare Info path for ios/android", () => {
for (const platform of ["ios", "android"]) {
const actualPrepareInfoPath = serviceTest.projectChangesService
.getPrepareInfoFilePath(serviceTest.getPlatformData(platform));
const expectedPrepareInfoPath = path.join(serviceTest.projectDir,
Constants.PLATFORMS_DIR_NAME,
platform,
".nsprepareinfo");
assert.equal(actualPrepareInfoPath, expectedPrepareInfoPath);
}
});
});
describe("Get Prepare Info", () => {
it("Returns empty if file path doesn't exists", () => {
for (const platform of ["ios", "android"]) {
const projectInfo = serviceTest.projectChangesService.getPrepareInfo(serviceTest.getPlatformData(platform));
assert.isNull(projectInfo);
}
});
it("Reads the Prepare Info correctly", () => {
const fs: FileSystem = serviceTest.resolve("fs");
for (const platform of ["ios", "android"]) {
// arrange
const prepareInfoPath = path.join(serviceTest.projectDir, Constants.PLATFORMS_DIR_NAME,
platform, ".nsprepareinfo");
const expectedPrepareInfo: IPrepareInfo = {
time: new Date().toString(),
bundle: true,
release: false,
changesRequireBuild: true,
changesRequireBuildTime: new Date().toString(),
iOSProvisioningProfileUUID: "provisioning_profile_test",
projectFileHash: "",
nativePlatformStatus: Constants.NativePlatformStatus.requiresPlatformAdd,
appFilesHashes: {}
};
fs.writeJson(prepareInfoPath, expectedPrepareInfo);
// act
const actualPrepareInfo = serviceTest.projectChangesService.getPrepareInfo(serviceTest.getPlatformData(platform));
// assert
assert.deepEqual(actualPrepareInfo, expectedPrepareInfo);
}
});
});
describe("Accumulates Changes From Project Services", () => {
it("accumulates changes from the project service", async () => {
const iOSChanges = await serviceTest.projectChangesService.checkForChanges(serviceTest.getPlatformData("ios"), serviceTest.projectData, <any>{
provision: undefined,
teamId: undefined
});
assert.isTrue(!!iOSChanges.signingChanged, "iOS signingChanged expected to be true");
});
});
describe("setNativePlatformStatus", () => {
it("creates prepare info and sets only the native platform status when there isn't an existing prepare info", () => {
for (const platform of ["ios", "android"]) {
serviceTest.projectChangesService.setNativePlatformStatus(serviceTest.getPlatformData(platform), { nativePlatformStatus: Constants.NativePlatformStatus.requiresPrepare });
const actualPrepareInfo = serviceTest.projectChangesService.getPrepareInfo(serviceTest.getPlatformData(platform));
assert.deepEqual(actualPrepareInfo, { nativePlatformStatus: Constants.NativePlatformStatus.requiresPrepare });
}
});
it(`shouldn't reset prepare info when native platform status is ${Constants.NativePlatformStatus.alreadyPrepared} and there is existing prepare info`, async () => {
for (const platform of ["ios", "android"]) {
await serviceTest.projectChangesService.checkForChanges(serviceTest.getPlatformData(platform), serviceTest.projectData, <any>{});
serviceTest.projectChangesService.savePrepareInfo(serviceTest.getPlatformData(platform));
const prepareInfo = serviceTest.projectChangesService.getPrepareInfo(serviceTest.getPlatformData(platform));
serviceTest.projectChangesService.setNativePlatformStatus(serviceTest.getPlatformData(platform), { nativePlatformStatus: Constants.NativePlatformStatus.alreadyPrepared });
const actualPrepareInfo = serviceTest.projectChangesService.getPrepareInfo(serviceTest.getPlatformData(platform));
prepareInfo.nativePlatformStatus = Constants.NativePlatformStatus.alreadyPrepared;
assert.deepEqual(actualPrepareInfo, prepareInfo);
}
});
_.each([Constants.NativePlatformStatus.requiresPlatformAdd, Constants.NativePlatformStatus.requiresPrepare], nativePlatformStatus => {
it(`should reset prepare info when native platform status is ${nativePlatformStatus} and there is existing prepare info`, async () => {
for (const platform of ["ios", "android"]) {
await serviceTest.projectChangesService.checkForChanges(serviceTest.getPlatformData(platform), serviceTest.projectData, <any>{});
serviceTest.projectChangesService.setNativePlatformStatus(serviceTest.getPlatformData(platform), { nativePlatformStatus: nativePlatformStatus });
const actualPrepareInfo = serviceTest.projectChangesService.getPrepareInfo(serviceTest.getPlatformData(platform));
assert.deepEqual(actualPrepareInfo, { nativePlatformStatus: nativePlatformStatus });
}
});
});
});
});