Skip to content

Commit

Permalink
Create test helper function testProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Feb 19, 2021
1 parent b28e1e2 commit dc1c0a1
Show file tree
Hide file tree
Showing 2 changed files with 408 additions and 400 deletions.
68 changes: 68 additions & 0 deletions src/TestHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { expect } from 'chai';
import { RopmModule } from './prefixer/RopmModule';
import type { RopmOptions } from './util';
import * as rokuDeploy from 'roku-deploy';
import { ModuleManager } from './prefixer/ModuleManager';
import { util } from './util';
export const sinon = createSandbox();

export const tempDir = path.join(process.cwd(), '.tmp');
Expand Down Expand Up @@ -188,3 +190,69 @@ export function pick(objects: any[], ...properties: string[]) {
}
return results;
}

/**
* Streamlined way to test the ModuleManager.process functionality
* The key is in the format `moduleName:path/to/file.ext`. You can specify an alias by doing `alias@moduleName:path/to/file.ext`
*/
export async function testProcess(args: Record<string, [source: string, expected?: string]>) {
const manager = new ModuleManager();
const hostDir = path.join(process.cwd(), '.tmp', 'hostApp');
const noprefixNpmAliases = (args['noprefixNpmAliases'] ?? []) as string[];
delete args['noprefixNpmAliases'];

const dependencies = [] as DepGraphNode[];
const expectedFileMap = new Map<string, any>();

for (const fileKey in args) {
const [source, expected] = args[fileKey];
const parts = fileKey.split(':');
let [dependencyName, filePath] = parts;
let alias = dependencyName;

//you can specify an alias by typing alias@originalName
const nameParts = dependencyName.split('@');
if (nameParts.length > 1) {
dependencyName = nameParts[1];
alias = nameParts[0];
}

let dep = dependencies.find(x => x.name === dependencyName);
if (!dep) {
dep = {
name: dependencyName,
alias: dependencyName !== alias ? alias : undefined,
_files: {}
};
dependencies.push(dep);
}
dep._files![filePath] = source;
//only set if we have an expected value (undefined means "don't test this one")
if (expected) {
expectedFileMap.set(filePath, expected);
}
}

manager.modules = createProjects(hostDir, hostDir, {
name: 'host',
dependencies: dependencies
});

manager.hostDependencies = await util.getModuleDependencies(hostDir);
manager.noprefixNpmAliases = noprefixNpmAliases;
await manager.process();

//copmare each output file to the expected
for (const dep of dependencies) {
for (const filePath in dep._files) {
const parts = filePath.split('/');
const baseFolder = parts.shift();
const remainingPath = parts.join('/');
const destinationPath = `${hostDir}/${baseFolder}/roku_modules/${dep.alias ?? dep.name}/${remainingPath}`;
//only test if we have an expected value
if (expectedFileMap.has(filePath)) {
fsEqual(destinationPath, expectedFileMap.get(filePath));
}
}
}
}
Loading

0 comments on commit dc1c0a1

Please sign in to comment.