Skip to content

Commit

Permalink
fix: Address feedback issues around testing
Browse files Browse the repository at this point in the history
Co-Authored-By: Norman Breau <norman@nbsolutions.ca>
  • Loading branch information
dpogue and breautek committed Aug 24, 2024
1 parent b1383dc commit 0a60231
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
8 changes: 4 additions & 4 deletions spec/ConfigChanges/ConfigFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ describe('ConfigFile tests', function () {
});

it('should return ios config.xml file path', function () {
ConfigFile.__set__('getIOSProjectname', () => 'iospath');
spyOn(ConfigFile, 'getIOSProjectname').and.returnValue('iospath');
const configPath = path.join('project_dir', 'iospath', 'config.xml');
expect(ConfigFile.resolveConfigFilePath('project_dir', 'ios', 'config.xml')).toBe(configPath);
});

it('should return osx config.xml file path', function () {
ConfigFile.__set__('getIOSProjectname', () => 'osxpath');
spyOn(ConfigFile, 'getIOSProjectname').and.returnValue('osxpath');
const configPath = path.join('project_dir', 'osxpath', 'config.xml');
expect(ConfigFile.resolveConfigFilePath('project_dir', 'osx', 'config.xml')).toBe(configPath);
});
Expand All @@ -111,7 +111,7 @@ describe('ConfigFile tests', function () {
const projName = 'XXX';
const expectedPlistPath = `${projName}${path.sep}${projName}-Info.plist`;

ConfigFile.__set__('getIOSProjectname', () => projName);
spyOn(ConfigFile, 'getIOSProjectname').and.returnValue(projName);
spyOn(require('fast-glob'), 'sync').and.returnValue([
`AAA/${projName}-Info.plist`,
`Pods/Target Support Files/Pods-${projName}/Info.plist`,
Expand All @@ -126,7 +126,7 @@ describe('ConfigFile tests', function () {
const projName = 'XXX';
const expectedPlistPath = path.join(projName, 'Info.plist');

ConfigFile.__set__('getIOSProjectname', () => projName);
spyOn(ConfigFile, 'getIOSProjectname').and.returnValue(projName);
spyOn(require('fast-glob'), 'sync').and.returnValue([
'AAA/Info.plist',
`Pods/Target Support Files/Pods-${projName}/Info.plist`,
Expand Down
57 changes: 29 additions & 28 deletions src/ConfigChanges/ConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const modules = {
get xml_helpers () { return require('../util/xml-helpers'); }
};

// Cache of project folder paths to Xcode project names
const xcodeprojMap = new Map();

/******************************************************************************
* ConfigFile class
*
Expand Down Expand Up @@ -155,6 +158,30 @@ class ConfigFile {

this.is_changed = true;
}

// Find out the real name of an iOS project
//
// This caches the project name for a given directory path, assuming that
// it won't change over the course of a single Cordova command invocation
static getIOSProjectname (project_dir) {
if (xcodeprojMap.has(project_dir)) {
return xcodeprojMap.get(project_dir);
}

const matches = modules.glob.sync('*.xcodeproj', { cwd: project_dir, onlyDirectories: true });

if (matches.length !== 1) {
const msg = matches.length === 0
? 'Does not appear to be an xcode project, no xcode project file'
: 'There are multiple *.xcodeproj dirs';

throw new Error(`${msg} in ${project_dir}`);
}

const projectName = path.basename(matches[0], '.xcodeproj');
xcodeprojMap.set(project_dir, projectName);
return projectName;
}
}

// Some config-file target attributes are not qualified with a full leading directory, or contain wildcards.
Expand All @@ -179,7 +206,7 @@ function resolveConfigFilePath (project_dir, platform, file) {

// [CB-5989] multiple Info.plist files may exist. default to Info.plist then $PROJECT_NAME-Info.plist
if (matches.length > 1 && file.includes('-Info.plist')) {
const projName = getIOSProjectname(project_dir);
const projName = ConfigFile.getIOSProjectname(project_dir);

// Try to find a unprefix Info.plist file first
let plistPath = path.join(project_dir, projName, 'Info.plist');
Expand Down Expand Up @@ -228,7 +255,7 @@ function resolveConfigFilePath (project_dir, platform, file) {
if (platform === 'ios' || platform === 'osx') {
filepath = path.join(
project_dir,
getIOSProjectname(project_dir),
ConfigFile.getIOSProjectname(project_dir),
'config.xml'
);
} else {
Expand All @@ -247,31 +274,6 @@ function resolveConfigFilePath (project_dir, platform, file) {
return filepath;
}

const xcodeprojMap = new Map();
// Find out the real name of an iOS project
//
// This caches the project name for a given directory path, assuming that it
// won't change over the course of a single Cordova command invocation
function getIOSProjectname (project_dir) {
if (xcodeprojMap.has(project_dir)) {
return xcodeprojMap.get(project_dir);
}

const matches = modules.glob.sync('*.xcodeproj', { cwd: project_dir, onlyDirectories: true });

if (matches.length !== 1) {
const msg = matches.length === 0
? 'Does not appear to be an xcode project, no xcode project file'
: 'There are multiple *.xcodeproj dirs';

throw new Error(`${msg} in ${project_dir}`);
}

const projectName = path.basename(matches[0], '.xcodeproj');
xcodeprojMap.set(project_dir, projectName);
return projectName;
}

// determine if a plist file is binary
// i.e. they start with the magic header "bplist"
// TODO: Remove in next major
Expand All @@ -281,5 +283,4 @@ function isBinaryPlist (filename) {

module.exports = ConfigFile;
module.exports.isBinaryPlist = util.deprecate(isBinaryPlist, 'isBinaryPlist is deprecated');
module.exports.getIOSProjectname = getIOSProjectname;
module.exports.resolveConfigFilePath = resolveConfigFilePath;

0 comments on commit 0a60231

Please sign in to comment.