Skip to content

fix(assets-generation): Do not fail in case some App_Resources are missing #3557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class ProjectDataService implements IProjectDataService {

private async getIOSAssetSubGroup(dirPath: string): Promise<IAssetSubGroup> {
const pathToContentJson = path.join(dirPath, AssetConstants.iOSResourcesFileName);
const content = <IAssetSubGroup>this.$fs.readJson(pathToContentJson);
const content = this.$fs.exists(pathToContentJson) && <IAssetSubGroup>this.$fs.readJson(pathToContentJson) || { images: [] };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen in case when pathToContentJson exists but the file is not a valid json? I'm expecting to throw an error in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I prefer to throw the error in this case as it is not expected to have a non-json file here.


const imageDefinitions = this.getImageDefinitions().ios;

Expand Down
61 changes: 58 additions & 3 deletions test/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Yok } from "../../lib/common/yok";
import { assert } from "chai";
import { ProjectDataService } from "../../lib/services/project-data-service";
import { LoggerStub } from "../stubs";
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER } from '../../lib/constants';
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, PACKAGE_JSON_FILE_NAME, AssetConstants } from '../../lib/constants';
import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants";
import { basename } from "path";

const CLIENT_NAME_KEY_IN_PROJECT_FILE = "nativescript";

Expand Down Expand Up @@ -53,19 +54,38 @@ const createTestInjector = (readTextData?: string): IInjector => {

readText: (filename: string, encoding?: IReadFileOptions | string): string => {
return readTextData;
}
},

exists: (filePath: string): boolean => basename(filePath) === PACKAGE_JSON_FILE_NAME,

readJson: (filePath: string): any => null,

enumerateFilesInDirectorySync: (directoryPath: string,
filterCallback?: (_file: string, _stat: IFsStats) => boolean,
opts?: { enumerateDirectories?: boolean, includeEmptyDirectories?: boolean },
foundFiles?: string[]): string[] => []
});

testInjector.register("logger", LoggerStub);

testInjector.register("projectDataService", ProjectDataService);

testInjector.register("androidResourcesMigrationService", {});
testInjector.register("androidResourcesMigrationService", {
hasMigrated: (appResourcesDir: string): boolean => true
});

testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);

testInjector.register("injector", testInjector);

testInjector.register("errors", {});

testInjector.register("projectHelper", {
sanitizeName: (appName: string): string => appName
});

testInjector.register("options", {});

return testInjector;
};

Expand Down Expand Up @@ -244,4 +264,39 @@ describe("projectDataService", () => {
assert.deepEqual(dataPassedToWriteJson, { dependencies: {} });
});
});

describe("getAssetsStructure", () => {
it("does not fail when App_Resources/Android and App_Resources/iOS do not exist", async () => {
const defaultEmptyData: any = {};
defaultEmptyData[CLIENT_NAME_KEY_IN_PROJECT_FILE] = {};
const testInjector = createTestInjector(JSON.stringify(defaultEmptyData));
const fs = testInjector.resolve<IFileSystem>("fs");
fs.readJson = (filePath: string): any => {
if (basename(filePath) === AssetConstants.imageDefinitionsFileName) {
return { android: {}, ios: {} };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case when imageDefinitionsFileName file does not exist, an error will be thrown. I'm wondering if we should check this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is part of CLI resources, so in case it does not exist, something terrible happened 😸

}

throw new Error(`Unable to read file ${filePath}`);
};

const projectDataService = testInjector.resolve<IProjectDataService>("projectDataService");
const assetStructure = await projectDataService.getAssetsStructure({ projectDir: "." });
const emptyAssetStructure: IAssetGroup = {
icons: {
images: []
},
splashBackgrounds: {
images: []
},
splashCenterImages: {
images: []
},
splashImages: {
images: []
}
};

assert.deepEqual(assetStructure, { ios: emptyAssetStructure, android: _.merge(_.cloneDeep(emptyAssetStructure), { splashImages: null }) });
});
});
});