@tywalk/pcf-helper
Version:
Command line helper for building and publishing PCF controls to Dataverse.
119 lines (118 loc) • 5.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const argumentUtil_1 = require("../util/argumentUtil");
jest.mock('fs');
jest.mock('os');
jest.mock('@tywalk/color-logger', () => ({
__esModule: true,
default: {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
},
}));
const mockFs = fs_1.default;
const mockOs = os_1.default;
function setFakeFiles(files) {
mockFs.existsSync.mockImplementation((p) => Object.prototype.hasOwnProperty.call(files, p.toString()));
mockFs.readFileSync.mockImplementation((p, _encoding) => {
const key = p.toString();
if (!(key in files))
throw new Error(`ENOENT: ${key}`);
return files[key];
});
}
describe('argumentUtil profile resolution', () => {
const HOME = '/home/tester';
const CWD = '/workspace/proj';
const projectCfgPath = path_1.default.join(CWD, 'pcf-helper.config.json');
beforeEach(() => {
jest.clearAllMocks();
mockOs.homedir.mockReturnValue(HOME);
jest.spyOn(process, 'cwd').mockReturnValue(CWD);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('resolvePathAndEnvironment', () => {
it('CLI flags win over profile values', () => {
const cfg = {
defaultProfile: 'dev',
profiles: { dev: { path: '/from/profile', environment: 'DevOrg' } },
};
setFakeFiles({ [projectCfgPath]: JSON.stringify(cfg) });
const result = (0, argumentUtil_1.resolvePathAndEnvironment)({ path: '/from/cli', environment: 'OverrideOrg' }, false);
expect(result.path).toBe('/from/cli');
expect(result.environment).toBe('OverrideOrg');
expect(result.profileName).toBe('dev');
});
it('falls back to profile values when CLI flags are omitted', () => {
const cfg = {
defaultProfile: 'dev',
profiles: { dev: { path: '/from/profile', environment: 'DevOrg' } },
};
setFakeFiles({ [projectCfgPath]: JSON.stringify(cfg) });
const result = (0, argumentUtil_1.resolvePathAndEnvironment)({}, false);
expect(result.path).toBe('/from/profile');
expect(result.environment).toBe('DevOrg');
});
it('uses an explicitly requested profile over defaultProfile', () => {
const cfg = {
defaultProfile: 'dev',
profiles: {
dev: { path: '/dev/path', environment: 'DevOrg' },
prod: { path: '/prod/path', environment: 'ProdOrg' },
},
};
setFakeFiles({ [projectCfgPath]: JSON.stringify(cfg) });
const result = (0, argumentUtil_1.resolvePathAndEnvironment)({ profile: 'prod' }, false);
expect(result.path).toBe('/prod/path');
expect(result.environment).toBe('ProdOrg');
expect(result.profileName).toBe('prod');
});
it('throws a helpful error when requested profile is unknown', () => {
const cfg = { profiles: { dev: {} } };
setFakeFiles({ [projectCfgPath]: JSON.stringify(cfg) });
expect(() => (0, argumentUtil_1.resolvePathAndEnvironment)({ profile: 'nope' }, false)).toThrow(/Profile "nope" not found/);
});
it('returns empty strings when no CLI flags, no profile, no config', () => {
setFakeFiles({});
const result = (0, argumentUtil_1.resolvePathAndEnvironment)({}, false);
expect(result.path).toBe('');
expect(result.environment).toBe('');
expect(result.profileName).toBeUndefined();
});
it('deprecated --env still works and shows up in resolved environment', () => {
setFakeFiles({});
const result = (0, argumentUtil_1.resolvePathAndEnvironment)({ env: 'LegacyOrg' }, true);
expect(result.environment).toBe('LegacyOrg');
});
});
describe('resolveProfileOnly', () => {
it('returns the resolved profile when one is configured', () => {
var _a;
const cfg = {
profiles: {
init: { publisherName: 'MyPub', publisherPrefix: 'mp' },
},
};
setFakeFiles({ [projectCfgPath]: JSON.stringify(cfg) });
const result = (0, argumentUtil_1.resolveProfileOnly)('init');
expect(result.profileName).toBe('init');
expect((_a = result.profile) === null || _a === void 0 ? void 0 : _a.publisherName).toBe('MyPub');
});
it('returns undefined profile when none requested and no default', () => {
setFakeFiles({});
const result = (0, argumentUtil_1.resolveProfileOnly)();
expect(result.profileName).toBeUndefined();
expect(result.profile).toBeUndefined();
});
});
});