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

Finish all planned tests

parent 4e3868d5
......@@ -46,7 +46,7 @@ Get-ChildItem .\* -Include ("*.docx", "*.xlsx", "*.pptx") -Recurse |
}
# Delete abandoned extractions
Get-ChildItem .\* -Filter *.git -Recurse |
Get-ChildItem . -Filter *.git -Recurse |
Foreach-Object {
If (-Not (Test-Path $_.FullName.TrimEnd(".git"))) {
$officePath = $_.FullName
......
......@@ -2,14 +2,6 @@
> Planned development:
## Finish `skip-unchanged.mjs` test
## Finish `verify-generated-warnings.mjs` test
## Finish `dispose-abandoned.mjs` test
## Finish `verify-pre-commit-hook.mjs` test
## Utilize GitHub/GitLab CI on top of the pre-commit hook
This is to capture online editor commits.
#!/usr/bin/env bash
# This hook also runs tests and is intended only for this repository.
# For repositories other than this, use `pre-commit.sh`.
#!/usr/bin/env bash
if [ ! -z "command -v powershell" ]
then
powershell cmd/run-tests.ps1
......
......@@ -4,11 +4,19 @@ import path from 'path';
import testWordBasic from './tests/word-basic';
import testExcelBasic from './tests/excel-basic';
import testPowerPointBasic from './tests/powerPoint-basic';
import testSkipUnchanged from './tests/skip-unchanged';
import testVerifyGeneratedWarnings from './tests/verify-generated-warnings';
import testDisposeAbandoned from './tests/dispose-abandoned';
import testVerifyPreCommitHook from './tests/verify-pre-commit-hook';
const tests = [
{ name: 'Word - basic', harness: testWordBasic },
{ name: 'Excel - basic', harness: testExcelBasic },
{ name: 'PowerPoint - basic', harness: testPowerPointBasic },
{ name: 'Skip unchanged', harness: testSkipUnchanged },
{ name: 'Verify generated warnings', harness: testVerifyGeneratedWarnings },
{ name: 'Dispose abandoned', harness: testDisposeAbandoned },
{ name: 'Verify pre-commit hook', harness: testVerifyPreCommitHook },
];
async function test() {
......
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript } from '../util.mjs';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript, unlinkFile, accessFile } from '../util.mjs';
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('docx', word => {
......@@ -9,7 +9,9 @@ export default async function() {
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
await invokePowerShellScript(directoryPath);
// TODO: Remove the `office.docx` file
await unlinkFile(filePath);
await invokePowerShellScript(directoryPath);
// TODO: Confirm `office.docx.git` directory is removed
if (await accessFile(filePath + '.git')) {
throw new Error('The directory still exists but should have been disposed.');
}
}
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript } from '../util.mjs';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript, commitGitStage, getLastModifiedDate } from '../util.mjs';
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('docx', word => {
......@@ -9,7 +9,10 @@ export default async function() {
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
await invokePowerShellScript(directoryPath);
// TODO: Remember date of `word/document.xml.txt`
await commitGitStage(directoryPath);
const modified = await getLastModifiedDate('word/document.xml.txt', filePath);
await invokePowerShellScript(directoryPath);
// TODO: Compare date of `word/document.xml.txt`
if (modified !== await getLastModifiedDate('word/document.xml.txt', filePath)) {
throw new Error(`Modified date changed but file did not. ${modified} !== ${await getLastModifiedDate('word/document.xml.txt', filePath)}`);
}
}
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript } from '../util.mjs';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript, inspectText, inspectTextSpans } from '../util.mjs';
const expectedWordDocumentXmlSpan = '<!-- Generated. Do not edit. https://github.com/TomasHubelbauer/modern-office-git-diff -->\r';
const expectedWordDocumentXmlTxtSpan = String.fromCharCode(65279) /* BOM*/ + '# Generated. Do not edit. https://github.com/TomasHubelbauer/modern-office-git-diff';
const expectedReadMeMdText = String.fromCharCode(65279) /* BOM*/ + `# \`office.docx\`
Generated. Do not edit. https://github.com/TomasHubelbauer/modern-office-git-diff\r\n`;
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('docx', word => {
......@@ -9,7 +15,19 @@ export default async function() {
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
await invokePowerShellScript(directoryPath);
// TODO: Check `README.md` for content match
// TODO: Check `word/document.xml.txt` for first line match
// TODO: Check `word/document.xml` for XML comment match
const readMeMdText = await inspectText('README.md', filePath);
if (readMeMdText !== expectedReadMeMdText) {
throw new Error(`README.md text did not match. >${readMeMdText}< versus >${expectedReadMeMdText}<`);
}
const wordDocumentXmlTxtSpans = await inspectTextSpans('word/document.xml.txt', filePath);
if (wordDocumentXmlTxtSpans[0] !== expectedWordDocumentXmlTxtSpan) {
throw new Error(`word/document.xml.txt span did not match. >${wordDocumentXmlTxtSpans[0]}< versus >${expectedWordDocumentXmlTxtSpan}<`);
}
const wordDocumentXmlSpans = await inspectTextSpans('word/document.xml', filePath);
if (wordDocumentXmlSpans[1] !== expectedWordDocumentXmlSpan) {
throw new Error(`word/document.xml span did not match. >${wordDocumentXmlSpans[1]}< versus >${expectedWordDocumentXmlSpan}<`);
}
}
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, invokePowerShellScript } from '../util.mjs';
import { generateTemporaryOfficeFile, initializeGitRepository, stageGitFile, commitGitStage, accessFile, clonePowerShellScript, copyToPreCommitHook } from '../util.mjs';
export default async function() {
const { directoryPath, filePath } = await generateTemporaryOfficeFile('docx', word => {
......@@ -8,7 +8,10 @@ export default async function() {
await initializeGitRepository(directoryPath);
await stageGitFile(filePath, directoryPath);
// TODO: Copy over `hook/pre-commit-development.sh`
// TODO: Run Git commit
// TODO: Verify `office.docx.git` directory exists
await clonePowerShellScript(directoryPath);
await copyToPreCommitHook(directoryPath, 'hook/pre-commit.sh');
await commitGitStage(directoryPath);
if (!(await accessFile(filePath + '.git'))) {
throw new Error('The directory did not get created, the hook did not run successfully.');
}
}
......@@ -6,6 +6,11 @@ import util from 'util';
import path from 'path';
const mkdtemp = util.promisify(fs.mkdtemp);
const stat = util.promisify(fs.stat);
const unlink = util.promisify(fs.unlink);
const access = util.promisify(fs.access);
const copyFile = util.promisify(fs.copyFile);
const mkdir = util.promisify(fs.mkdir);
const exec = util.promisify(child_process.exec);
const readFile = util.promisify(fs.readFile);
......@@ -50,6 +55,16 @@ export async function stageGitFile(filePath, workingDirectoryPath) {
}
}
export async function commitGitStage(workingDirectoryPath) {
try {
const { stdout, stderr } = await exec(`git commit -m "Commit changed files"`, { 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');
......@@ -61,6 +76,36 @@ export async function invokePowerShellScript(workingDirectoryPath) {
}
}
export async function inspectText(archiveFilePath, officeFilePath) {
return String(await readFile(path.join(officeFilePath + '.git', archiveFilePath))); // BOM?
}
export async function inspectTextSpans(archiveFilePath, officeFilePath) {
return String(await readFile(path.join(officeFilePath + '.git', archiveFilePath))).split('\n');
}
export async function getLastModifiedDate(archiveFilePath, officeFilePath) {
return (await stat(path.join(officeFilePath + '.git', archiveFilePath))).mtimeMs;
}
export async function unlinkFile(officeFilePath) {
await unlink(officeFilePath);
}
export async function accessFile(directoryPath) {
try {
await access(directoryPath);
return true;
} catch (error) {
return false;
}
}
export async function copyToPreCommitHook(directoryPath, relativeHookFilePath) {
await copyFile(path.join(process.cwd(), '..', relativeHookFilePath), path.join(directoryPath, '.git/hooks/pre-commit'));
}
export async function clonePowerShellScript(directoryPath) {
await mkdir(path.join(directoryPath, 'cmd'));
await copyFile(path.join(process.cwd(), '../cmd/version-office-files.ps1'), path.join(directoryPath, 'cmd/version-office-files.ps1'));
}
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