@tywalk/pcf-helper
Version:
Command line helper for building and publishing PCF controls to Dataverse.
134 lines (133 loc) • 6.84 kB
JavaScript
"use strict";
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 configUtil_1 = require("../util/configUtil");
jest.mock('@tywalk/color-logger', () => ({
__esModule: true,
default: {
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
},
}));
describe('writeProfile', () => {
let tmpRoot;
beforeEach(() => {
// Real filesystem temp dir — this lets us exercise actual atomic rename semantics.
tmpRoot = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'pcf-helper-writeProfile-'));
});
afterEach(() => {
try {
fs_1.default.rmSync(tmpRoot, { recursive: true, force: true });
}
catch (_a) {
// best-effort cleanup
}
});
it('creates a new project config file when none exists', () => {
var _a;
const cwd = tmpRoot;
const result = (0, configUtil_1.writeProfile)('dev', { environment: 'DevOrg', publisherName: 'Tyler W', publisherPrefix: 'tyw' }, { cwd });
expect(result.createdFile).toBe(true);
expect(result.replacedProfile).toBe(false);
expect(result.filePath).toBe(path_1.default.join(cwd, 'pcf-helper.config.json'));
const onDisk = JSON.parse(fs_1.default.readFileSync(result.filePath, 'utf8'));
expect((_a = onDisk.profiles) === null || _a === void 0 ? void 0 : _a.dev).toEqual({
environment: 'DevOrg',
publisherName: 'Tyler W',
publisherPrefix: 'tyw',
});
});
it('merges a new profile into an existing config without touching other profiles', () => {
var _a, _b;
const cwd = tmpRoot;
const configPath = path_1.default.join(cwd, 'pcf-helper.config.json');
const existing = {
defaultProfile: 'dev',
profiles: {
dev: { environment: 'DevOrg' },
},
session: { remoteEnvironmentUrl: 'https://dev.example.com' },
};
fs_1.default.writeFileSync(configPath, JSON.stringify(existing, null, 2));
const result = (0, configUtil_1.writeProfile)('prod', { environment: 'ProdOrg' }, { cwd });
expect(result.createdFile).toBe(false);
expect(result.replacedProfile).toBe(false);
const onDisk = JSON.parse(fs_1.default.readFileSync(result.filePath, 'utf8'));
expect((_a = onDisk.profiles) === null || _a === void 0 ? void 0 : _a.dev).toEqual({ environment: 'DevOrg' });
expect((_b = onDisk.profiles) === null || _b === void 0 ? void 0 : _b.prod).toEqual({ environment: 'ProdOrg' });
expect(onDisk.defaultProfile).toBe('dev');
expect(onDisk.session).toEqual({ remoteEnvironmentUrl: 'https://dev.example.com' });
});
it('refuses to overwrite an existing profile without force', () => {
var _a;
const cwd = tmpRoot;
const configPath = path_1.default.join(cwd, 'pcf-helper.config.json');
const existing = {
profiles: { dev: { environment: 'DevOrg' } },
};
fs_1.default.writeFileSync(configPath, JSON.stringify(existing, null, 2));
expect(() => (0, configUtil_1.writeProfile)('dev', { environment: 'NewDevOrg' }, { cwd })).toThrow(/already exists/);
// File must be unchanged.
const onDisk = JSON.parse(fs_1.default.readFileSync(configPath, 'utf8'));
expect((_a = onDisk.profiles) === null || _a === void 0 ? void 0 : _a.dev).toEqual({ environment: 'DevOrg' });
});
it('overwrites an existing profile when force is set', () => {
var _a;
const cwd = tmpRoot;
const configPath = path_1.default.join(cwd, 'pcf-helper.config.json');
const existing = {
profiles: { dev: { environment: 'DevOrg' } },
};
fs_1.default.writeFileSync(configPath, JSON.stringify(existing, null, 2));
const result = (0, configUtil_1.writeProfile)('dev', { environment: 'NewDevOrg', publisherName: 'Tyler' }, { cwd, force: true });
expect(result.replacedProfile).toBe(true);
const onDisk = JSON.parse(fs_1.default.readFileSync(configPath, 'utf8'));
expect((_a = onDisk.profiles) === null || _a === void 0 ? void 0 : _a.dev).toEqual({
environment: 'NewDevOrg',
publisherName: 'Tyler',
});
});
it('writes defaultProfile when setDefault is passed', () => {
const cwd = tmpRoot;
const result = (0, configUtil_1.writeProfile)('prod', { environment: 'ProdOrg' }, { cwd, setDefault: true });
const onDisk = JSON.parse(fs_1.default.readFileSync(result.filePath, 'utf8'));
expect(onDisk.defaultProfile).toBe('prod');
});
it('creates the parent directory for the global config on first use', () => {
var _a;
const fakeHome = path_1.default.join(tmpRoot, 'fake-home');
// Mock homedir via jest.spyOn so getGlobalConfigPath returns a path inside tmpRoot.
const spy = jest.spyOn(os_1.default, 'homedir').mockReturnValue(fakeHome);
try {
// Parent dir (~/.pcf-helper) intentionally does not exist.
expect(fs_1.default.existsSync(path_1.default.join(fakeHome, '.pcf-helper'))).toBe(false);
const result = (0, configUtil_1.writeProfile)('dev', { environment: 'DevOrg' }, { global: true });
expect(result.filePath).toBe(path_1.default.join(fakeHome, '.pcf-helper', 'config.json'));
expect(result.createdFile).toBe(true);
expect(fs_1.default.existsSync(result.filePath)).toBe(true);
const onDisk = JSON.parse(fs_1.default.readFileSync(result.filePath, 'utf8'));
expect((_a = onDisk.profiles) === null || _a === void 0 ? void 0 : _a.dev).toEqual({ environment: 'DevOrg' });
}
finally {
spy.mockRestore();
}
});
it('throws when profile name is empty', () => {
expect(() => (0, configUtil_1.writeProfile)('', { environment: 'X' }, { cwd: tmpRoot })).toThrow(/name is required/);
expect(() => (0, configUtil_1.writeProfile)(' ', { environment: 'X' }, { cwd: tmpRoot })).toThrow(/name is required/);
});
it('leaves no temp file behind on success', () => {
const cwd = tmpRoot;
(0, configUtil_1.writeProfile)('dev', { environment: 'DevOrg' }, { cwd });
const entries = fs_1.default.readdirSync(cwd);
const tmpFiles = entries.filter((f) => f.startsWith('pcf-helper.config.json.tmp'));
expect(tmpFiles).toEqual([]);
});
});