Commit 8e121f4d authored by Tomáš Hübelbauer's avatar Tomáš Hübelbauer

Pull test util functions out to enable more test types

parent d7382770
import officegen from 'officegen';
import fs from 'fs';
import os from 'os';
import path from 'path';
import child_process from 'child_process';
import util from 'util';
import testWordBasic from './tests/word-basic';
import testExcelBasic from './tests/excel-basic';
import testPowerPointBasic from './tests/powerPoint-basic';
const mkdtemp = util.promisify(fs.mkdtemp);
const exec = util.promisify(child_process.exec);
const tests = [
{ name: 'Word - basic', harness: testWordBasic },
{ name: 'Excel - basic', harness: testExcelBasic },
{ name: 'PowerPoint - basic', harness: testPowerPointBasic },
];
async function test() {
console.log('Running Office tests…\n');
const results = [];
results.push(await run(testWordBasic, 'docx', 'Word - basic'));
results.push(await run(testExcelBasic, 'xlsx', 'Excel - basic'));
results.push(await run(testPowerPointBasic, 'pptx', 'PowerPoint - basic'));
const results = [];
for (const { name, harness } of tests) {
console.log(`Running ${name}…\n`);
try {
await harness();
results.push({ name });
} catch (error) {
results.push({ name, error });
}
}
console.log('\n\n');
for (const result of results) {
console.log(result);
if (result.error) {
console.log(`'${result.name}' failed with an error.`, result.error);
} else {
console.log(`'${result.name}' succeeded.`);
}
}
}
test();
// Change to decorator once supported in NodeJS
async function run(harness, type, name) {
console.log('Test:' + name);
const office = officegen({ type });
let test;
const temporaryDirectoryPath = await mkdtemp(path.join(os.tmpdir(), 'modern-office-diff-git'));
const officeFilePath = path.join(temporaryDirectoryPath, 'office.' + type);
const scriptFilePath = path.join(process.cwd(), '../cmd/version-office-files.ps1');
// Populate the Office file.
try {
test = harness(office);
} catch (error) {
console.log('Failed to constuct the Office file.', error);
}
// Initialize a Git repository.
try {
const { stdout, stderr } = await exec(`git init`, { cwd: temporaryDirectoryPath });
console.log(stdout);
console.error(stderr);
} catch (error) {
console.log('Failed to initialize a Git repository.', error);
}
// Generate the Office file and stage it for `git diff`.
try {
const promise = new Promise((resolve, reject) => {
office.on('finalize', resolve);
office.on('error', reject);
});
office.generate(fs.createWriteStream(officeFilePath));
await promise;
} catch (error) {
console.log('Failed to generate the Office file.', error);
}
// Stage the Office file.
try {
const { stdout, stderr } = await exec(`git add "${officeFilePath}"`, { cwd: temporaryDirectoryPath });
console.log(stdout);
console.error(stderr);
} catch (error) {
console.log('Failed to stage the Office file.', error);
}
// Execute the PowerShell script.
try {
const { stdout, stderr } = await exec(`powershell ${scriptFilePath}`, { cwd: temporaryDirectoryPath });
console.log(stdout);
console.error(stderr);
} catch (error) {
console.log('Failed to execute the Office script.', error);
}
console.log(`Testing ${officeFilePath}…\n`);
try {
test(path.join(temporaryDirectoryPath, `office.${type}.git`));
return `Succeeded`;
} catch (error) {
return `Failed with an error ${error.message}`;
}
}
import fs from 'fs';
import util from 'util';
import path from 'path';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript, inspectTextSpans } from '../util.mjs';
const readFile = util.promisify(fs.readFile);
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('xlsx', excel => {
const s1 = excel.makeNewSheet('sheet1');
s1.setCell('A1', 'Hello,');
s1.setCell('B2', 'World!');
const s2 = excel.makeNewSheet('sheet2');
s1.setCell('C3', 'Hi,');
s1.setCell('D4', 'here');
s1.setCell('D4', 'there');
});
export default function(excel) {
const s1 = excel.makeNewSheet('sheet1');
s1.setCell('A1', 'Hello,');
s1.setCell('B2', 'World!');
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
await invokePowerShellScript(directoryPath);
const spans = await inspectTextSpans('xl/sharedStrings.xml.txt', filePath);
const s2 = excel.makeNewSheet('sheet2');
s1.setCell('C3', 'Hi,');
s1.setCell('D4', 'here');
s1.setCell('D4', 'there');
return async (dir) => {
const spans = String(await readFile(path.join(dir, 'xl/sharedStrings.xml.txt'))).split('\n');
if (spans[1] !== 'Hello,' || spans[2] !== 'World!' || spans[3] !== 'Hi,' || spans[4] !== 'there' || spans[5] !== '\r') {
throw new Error('The resulting TXT file did not have matching spans to what was inserted.\n' + spans.join('\n'));
}
};
if (spans[1] !== 'Hello,' || spans[2] !== 'World!' || spans[3] !== 'Hi,' || spans[4] !== 'there' || spans[5] !== '\r') {
throw new Error('The resulting TXT file did not have matching spans to what was inserted.\n' + spans.join('\n'));
}
}
import fs from 'fs';
import util from 'util';
import path from 'path';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript, inspectTextSpans } from '../util.mjs';
const readFile = util.promisify(fs.readFile);
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('pptx', powerPoint => {
const slide = powerPoint.makeNewSlide();
slide.addText('Hello,');
slide.addText('World!');
const titleSlide = powerPoint.makeTitleSlide();
titleSlide.setTitle('Hello hello hello');
titleSlide.setSubTitle('It is a-me Mario');
});
export default function(powerPoint) {
const slide = powerPoint.makeNewSlide();
slide.addText('Hello,');
slide.addText('World!');
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
await invokePowerShellScript(directoryPath);
const titleSlide = powerPoint.makeTitleSlide();
titleSlide.setTitle('Hello hello hello');
titleSlide.setSubTitle('It is a-me Mario');
const slideSpans = await inspectTextSpans('ppt/slides/slide1.xml.txt', filePath);
if (slideSpans[1] !== 'Hello,' || slideSpans[2] !== 'World!' || slideSpans[3] !== '\r') {
throw new Error('The resulting TXT file for slide did not have matching spans to what was inserted.\n', slideSpans.join('\n'));
}
return async (dir) => {
const slideSpans = String(await readFile(path.join(dir, 'ppt/slides/slide1.xml.txt'))).split('\n');
if (slideSpans[1] !== 'Hello,' || slideSpans[2] !== 'World!' || slideSpans[3] !== '\r') {
throw new Error('The resulting TXT file for slide did not have matching spans to what was inserted.\n', slideSpans.join('\n'));
}
const titleSlideSpans = String(await readFile(path.join(dir, 'ppt/slides/slide2.xml.txt'))).split('\n');
if (titleSlideSpans[1] !== 'Hello hello hello' || titleSlideSpans[2] !== 'It is a-me Mario' || titleSlideSpans[5] !== '\r') {
throw new Error('The resulting TXT file for title slide did not have matching spans to what was inserted.\n', titleSlideSpans.join('\n'));
}
};
const titleSlideSpans = await inspectTextSpans('ppt/slides/slide2.xml.txt', filePath);
if (titleSlideSpans[1] !== 'Hello hello hello' || titleSlideSpans[2] !== 'It is a-me Mario' || titleSlideSpans[5] !== '\r') {
throw new Error('The resulting TXT file for title slide did not have matching spans to what was inserted.\n', titleSlideSpans.join('\n'));
}
}
import fs from 'fs';
import util from 'util';
import path from 'path';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript, inspectTextSpans } from '../util.mjs';
const readFile = util.promisify(fs.readFile);
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('docx', word => {
const p1 = word.createP();
p1.addText('Hello,');
p1.addText('World!');
const p2 = word.createP();
p2.addText('Hi,');
p2.addText('it is me, Tom!');
});
export default function(word) {
const p1 = word.createP();
p1.addText('Hello,');
p1.addText('World!');
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
await invokePowerShellScript(directoryPath);
const spans = await inspectTextSpans('word/document.xml.txt', filePath);
const p2 = word.createP();
p2.addText('Hi,');
p2.addText('it is me, Tom!');
return async (dir) => {
const spans = String(await readFile(path.join(dir, 'word/document.xml.txt'))).split('\n');
if (spans[1] !== 'Hello,' || spans[2] !== 'World!' || spans[3] !== 'Hi,' || spans[4] !== 'it is me, Tom!' || spans[5] !== '\r') {
throw new Error('The resulting TXT file did not have matching spans to what was inserted.\n' + spans.join('\n'));
}
};
if (spans[1] !== 'Hello,' || spans[2] !== 'World!' || spans[3] !== 'Hi,' || spans[4] !== 'it is me, Tom!' || spans[5] !== '\r') {
throw new Error('The resulting TXT file did not have matching spans to what was inserted.\n' + spans.join('\n'));
}
}
import officegen from 'officegen';
import os from 'os';
import fs from 'fs';
import child_process from 'child_process';
import util from 'util';
import path from 'path';
const mkdtemp = util.promisify(fs.mkdtemp);
const exec = util.promisify(child_process.exec);
const readFile = util.promisify(fs.readFile);
export async function generateTemporaryOfficeFile(type, setup) {
const office = officegen({ type });
setup(office);
const directoryPath = await mkdtemp(path.join(os.tmpdir(), 'modern-office-diff-git'));
const filePath = path.join(directoryPath, 'office.' + type);
const promise = new Promise((resolve, reject) => {
office.on('finalize', resolve);
office.on('error', reject);
});
office.generate(fs.createWriteStream(filePath));
await promise;
return {
directoryPath,
filePath
};
}
export async function initializeGitRepository(directoryPath) {
try {
const { stdout, stderr } = await exec(`git init`, { cwd: directoryPath });
console.log(stdout);
console.error(stderr);
} catch (error) {
console.log(error);
}
}
export async function stageGitFile(filePath, workingDirectoryPath) {
try {
const { stdout, stderr } = await exec(`git add "${filePath}"`, { cwd: workingDirectoryPath });
console.log(stdout);
console.error(stderr);
} catch (error) {
console.log(error);
}
}
export async function invokePowerShellScript(workingDirectoryPath) {
try {
const scriptFilePath = path.join(process.cwd(), '../cmd/version-office-files.ps1');
const { stdout, stderr } = await exec(`powershell ${scriptFilePath}`, { cwd: workingDirectoryPath });
console.log(stdout);
console.error(stderr);
} catch (error) {
console.log(error);
}
}
export async function inspectTextSpans(archiveFilePath, officeFilePath) {
return String(await readFile(path.join(officeFilePath + '.git', archiveFilePath))).split('\n');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment