Skip to content

Commit 7dcfa3a

Browse files
committed
chore(tests): fix tests related to fetching app_resources from nsconfig
1 parent 422835c commit 7dcfa3a

8 files changed

+44
-25
lines changed

lib/project-data.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as constants from "./constants";
22
import * as path from "path";
3-
import { cache } from "./common/decorators";
43
import { EOL } from "os";
54

65
interface IProjectType {
@@ -87,9 +86,8 @@ export class ProjectData implements IProjectData {
8786
this.$errors.fail("No project found at or above '%s' and neither was a --path specified.", projectDir || this.$options.path || currentDir);
8887
}
8988

90-
@cache()
9189
public getAppResourcesDirectoryPath(projectDir?: string): string {
92-
if (!!projectDir) {
90+
if (!projectDir) {
9391
projectDir = this.projectDir;
9492
}
9593

lib/providers/project-files-provider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { ProjectFilesProviderBase } from "../common/services/project-files-provi
66
export class ProjectFilesProvider extends ProjectFilesProviderBase {
77
constructor(private $platformsData: IPlatformsData,
88
$mobileHelper: Mobile.IMobileHelper,
9-
$options:IOptions) {
10-
super($mobileHelper, $options);
9+
$options: IOptions) {
10+
super($mobileHelper, $options);
1111
}
1212

13-
private static INTERNAL_NONPROJECT_FILES = [ "**/*.ts" ];
13+
private static INTERNAL_NONPROJECT_FILES = ["**/*.ts"];
1414

1515
public mapFilePath(filePath: string, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): string {
1616
const platformData = this.$platformsData.getPlatformData(platform.toLowerCase(), projectData);
@@ -23,14 +23,14 @@ export class ProjectFilesProvider extends ProjectFilesProviderBase {
2323
mappedFilePath = path.join(platformData.appDestinationDirectoryPath, path.relative(projectData.projectDir, parsedFilePath));
2424
}
2525

26-
const appResourcesDirectoryPath = projectData.appDirectoryPath;
26+
const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
2727
const platformSpecificAppResourcesDirectoryPath = path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName);
2828
if (parsedFilePath.indexOf(appResourcesDirectoryPath) > -1 && parsedFilePath.indexOf(platformSpecificAppResourcesDirectoryPath) === -1) {
2929
return null;
3030
}
3131

3232
if (parsedFilePath.indexOf(platformSpecificAppResourcesDirectoryPath) > -1) {
33-
const appResourcesRelativePath = path.relative(path.join(appResourcesDirectoryPath,
33+
const appResourcesRelativePath = path.relative(path.join(projectData.getAppResourcesDirectoryPath(),
3434
platformData.normalizedPlatformName), parsedFilePath);
3535
mappedFilePath = path.join(platformData.platformProjectService.getAppResourcesDestinationDirectoryPath(projectData), appResourcesRelativePath);
3636
}

test/ios-entitlements-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("IOSEntitlements Service Tests", () => {
1919
const testInjector = new yok.Yok();
2020

2121
testInjector.register('platformsData', stubs.PlatformsDataStub);
22+
testInjector.register('projectData', stubs.ProjectDataStub);
2223
testInjector.register("logger", stubs.LoggerStub);
2324
testInjector.register('iOSEntitlementsService', IOSEntitlementsService);
2425

@@ -46,7 +47,7 @@ describe("IOSEntitlements Service Tests", () => {
4647
injector = createTestInjector();
4748

4849
platformsData = injector.resolve("platformsData");
49-
projectData = <IProjectData>platformsData.getPlatformData();
50+
projectData = injector.resolve("projectData");
5051
projectData.projectName = 'testApp';
5152

5253
projectData.platformsDir = temp.mkdirSync("platformsDir");

test/ios-project-service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { NodePackageManager } from "../lib/node-package-manager";
3333
import { assert } from "chai";
3434
import { IOSProvisionService } from "../lib/services/ios-provision-service";
3535
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
36+
import { ProjectDataStub } from "../test/stubs";
3637
import temp = require("temp");
3738

3839
temp.track();
@@ -64,12 +65,13 @@ function createTestInjector(projectPath: string, projectName: string): IInjector
6465
testInjector.register("iOSEntitlementsService", IOSEntitlementsService);
6566
testInjector.register("logger", LoggerLib.Logger);
6667
testInjector.register("options", OptionsLib.Options);
67-
testInjector.register("projectData", {
68+
const projectData = Object.assign({}, ProjectDataStub, {
6869
platformsDir: path.join(projectPath, "platforms"),
6970
projectName: projectName,
7071
projectPath: projectPath,
7172
projectFilePath: path.join(projectPath, "package.json")
7273
});
74+
testInjector.register("projectData", projectData);
7375
testInjector.register("projectHelper", {});
7476
testInjector.register("xcodeSelectService", {});
7577
testInjector.register("staticConfig", ConfigLib.StaticConfig);

test/platform-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ describe('Platform Service Tests', () => {
445445
const projectData = testInjector.resolve("projectData");
446446
projectData.projectDir = testDirData.tempFolder;
447447
projectData.appDirectoryPath = testDirData.appFolderPath;
448-
projectData.getAppResourcesDirectoryPath = () => path.join(testDirData.appFolderPath, "App_Resources");
449448
projectData.projectName = "app";
450449

451450
platformService = testInjector.resolve("platformService");

test/project-files-provider.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Yok } from "../lib/common/yok";
22
import { ProjectFilesProvider } from "../lib/providers/project-files-provider";
3+
import * as stubs from "./stubs";
34
import { assert } from "chai";
45
import * as path from "path";
56

@@ -14,6 +15,8 @@ function createTestInjector(): IInjector {
1415
platformNames: ["Android", "iOS"]
1516
});
1617

18+
testInjector.register('projectData', stubs.ProjectDataStub);
19+
1720
testInjector.register("platformsData", {
1821
getPlatformData: (platform: string) => {
1922
return {
@@ -26,10 +29,6 @@ function createTestInjector(): IInjector {
2629
},
2730
});
2831

29-
testInjector.register("projectData", {
30-
projectDir: projectDir
31-
});
32-
3332
testInjector.register("options", { release: false });
3433

3534
return testInjector;
@@ -38,9 +37,12 @@ function createTestInjector(): IInjector {
3837
describe("project-files-provider", () => {
3938
let testInjector: IInjector;
4039
let projectFilesProvider: IProjectFilesProvider;
40+
let projectData: IProjectData;
4141

4242
beforeEach(() => {
4343
testInjector = createTestInjector();
44+
projectData = testInjector.resolve("projectData");
45+
projectData.projectDir = projectDir;
4446
projectFilesProvider = testInjector.resolve(ProjectFilesProvider);
4547
});
4648

@@ -56,37 +58,31 @@ describe("project-files-provider", () => {
5658

5759
describe("mapFilePath", () => {
5860
it("returns file path from prepared project when path from app dir is passed", () => {
59-
const projectData: IProjectData = testInjector.resolve("projectData");
6061
const mappedFilePath = projectFilesProvider.mapFilePath(path.join(appSourceDir, "test.js"), "android", projectData, {});
6162
assert.deepEqual(mappedFilePath, path.join(appDestinationDirectoryPath, "app", "test.js"));
6263
});
6364

6465
it("returns file path from prepared project when path from app/App_Resources/platform dir is passed", () => {
65-
const projectData: IProjectData = testInjector.resolve("projectData");
6666
const mappedFilePath = projectFilesProvider.mapFilePath(path.join(appSourceDir, "App_Resources", "android", "test.js"), "android", projectData, {});
6767
assert.deepEqual(mappedFilePath, path.join(appResourcesDestinationDirectoryPath, "test.js"));
6868
});
6969

7070
it("returns null when path from app/App_Resources/android dir is passed and iOS platform is specified", () => {
71-
const projectData: IProjectData = testInjector.resolve("projectData");
7271
const mappedFilePath = projectFilesProvider.mapFilePath(path.join(appSourceDir, "App_Resources", "android", "test.js"), "iOS", projectData, {});
7372
assert.deepEqual(mappedFilePath, null);
7473
});
7574

7675
it("returns null when path from app/App_Resources/ dir (not platform specific) is passed", () => {
77-
const projectData: IProjectData = testInjector.resolve("projectData");
7876
const mappedFilePath = projectFilesProvider.mapFilePath(path.join(appSourceDir, "App_Resources", "test.js"), "android", projectData, {});
7977
assert.deepEqual(mappedFilePath, null);
8078
});
8179

8280
it("returns file path from prepared project when path from app dir is passed and it contains platform in its name", () => {
83-
const projectData: IProjectData = testInjector.resolve("projectData");
8481
const mappedFilePath = projectFilesProvider.mapFilePath(path.join(appSourceDir, "test.android.js"), "android", projectData, {});
8582
assert.deepEqual(mappedFilePath, path.join(appDestinationDirectoryPath, "app", "test.js"));
8683
});
8784

8885
it("returns file path from prepared project when path from app dir is passed and it contains configuration in its name", () => {
89-
const projectData: IProjectData = testInjector.resolve("projectData");
9086
const mappedFilePath = projectFilesProvider.mapFilePath(path.join(appSourceDir, "test.debug.js"), "android", projectData, {});
9187
assert.deepEqual(mappedFilePath, path.join(appDestinationDirectoryPath, "app", "test.js"));
9288
});

test/project-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ProjectIntegrationTest {
133133
this.testInjector.register("projectHelper", ProjectHelperLib.ProjectHelper);
134134
this.testInjector.register("projectTemplatesService", ProjectTemplatesService);
135135
this.testInjector.register("projectNameValidator", mockProjectNameValidator);
136-
this.testInjector.register("projectData", {});
136+
this.testInjector.register("projectData", stubs.ProjectDataStub);
137137

138138
this.testInjector.register("fs", FileSystem);
139139
this.testInjector.register("projectDataService", ProjectDataServiceLib.ProjectDataService);

test/stubs.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import * as util from "util";
44
import * as chai from "chai";
55
import { EventEmitter } from "events";
66

7+
import * as fs from "fs";
8+
import * as path from "path";
9+
import * as constants from "./../lib/constants";
10+
711
export class LoggerStub implements ILogger {
812
setLevel(level: string): void { }
913
getLevel(): string { return undefined; }
@@ -244,17 +248,36 @@ export class ProjectDataStub implements IProjectData {
244248
get platformsDir(): string {
245249
return "";
246250
}
251+
set platformsDir(value) {
252+
}
247253
projectFilePath: string;
248254
projectId: string;
249255
dependencies: any;
250256
appDirectoryPath: string;
251257
devDependencies: IStringDictionary;
252258
projectType: string;
253-
initializeProjectData(projectDir?: string): void {
259+
public initializeProjectData(projectDir?: string): void {
254260
this.projectDir = this.projectDir || projectDir;
255261
}
256-
getAppResourcesDirectoryPath(projectDir?: string): string {
257-
return this.projectDir + "/App_Resources";
262+
public getAppResourcesDirectoryPath(projectDir?: string): string {
263+
if (!projectDir) {
264+
projectDir = this.projectDir;
265+
}
266+
267+
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
268+
let absoluteAppResourcesDirPath: string;
269+
270+
if (fs.existsSync(configNSFilePath)) {
271+
const configNS = JSON.parse(fs.readFileSync(configNSFilePath).toString());
272+
273+
if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
274+
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
275+
276+
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
277+
}
278+
}
279+
280+
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
258281
}
259282
}
260283

0 commit comments

Comments
 (0)