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

Implement basis for writing tests

parent f747759b
......@@ -2,6 +2,11 @@
> Development log.
## 2018-01-02
Implemented tests by the way of generating Word, Excel, PowerPoint files and running the script on them, then checking
the resulting extracted files for exepcted strings.
## 2018-01-01
Implemented skipping processing unchanged Office files (using `git diff` to tell if a file has changes).
......
......@@ -2,24 +2,10 @@
> Planned development:
## Add tests
- Use [officegen](https://www.npmjs.com/package/officegen) to generate files in a temp directory
- Insert spans with various formatting options depending on the format kind
- Run the PowerShell script on the generated files
- Check the text-only generated files to see if captured spans are there
- Also test checking for unchanged files etc.
`node --experimental-modules test.mjs`
```js
// https://www.npmjs.com/package/officegen
import codegen from 'codegen';
const word = codegen('0133…');
// Change `on('finalize') and on('error') to deferred promises to utilize `async`/`await`
```
## Add MIT license file
## Verify PowerShell script runs okay on non-Windows PowerShell
I suspect there will be a problem with relying on the XML functionality which is likely to be missing when not on Windows (is .NET).
## Add more tests of varying complexity for all of Word, Excel and PowerPoint
node_modules
# Modern Office Git Diff Test
**Prerequisites:**
- Node 9.3.0
- Yarn 1.3.2
```sh
nvm use
yarn
yarn start
```
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';
const mkdtemp = util.promisify(fs.mkdtemp);
const exec = util.promisify(child_process.exec);
async function test() {
console.log('Running Office tests…\n');
await run(testWordBasic, 'docx', 'Word - basic');
// TODO: Add more tests.
}
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}…`);
try {
test(path.join(temporaryDirectoryPath, `office.${type}.git`));
console.log('…done.\n');
} catch (error) {
console.log('…failed with error', error);
}
}
{
"name": "modern-office-git-diff-test",
"version": "1.0.0",
"main": "index.mjs",
"repository": "https://github.com/TomasHubelbauer/modern-office-git-diff",
"author": "Tomas Hubelbauer",
"license": "MIT",
"scripts": {
"start": "node --experimental-modules index.mjs"
},
"devDependencies": {
"officegen": "^0.4.5"
},
"dependencies": {}
}
import fs from 'fs';
import util from 'util';
import path from 'path';
const readFile = util.promisify(fs.readFile);
export default function(word) {
const p1 = word.createP();
p1.addText('Hello,');
p1.addText('World!');
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.');
}
}
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
archiver-utils@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/archiver-utils/-/archiver-utils-1.3.0.tgz#e50b4c09c70bf3d680e32ff1b7994e9f9d895174"
dependencies:
glob "^7.0.0"
graceful-fs "^4.1.0"
lazystream "^1.0.0"
lodash "^4.8.0"
normalize-path "^2.0.0"
readable-stream "^2.0.0"
archiver@~1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/archiver/-/archiver-1.3.0.tgz#4f2194d6d8f99df3f531e6881f14f15d55faaf22"
dependencies:
archiver-utils "^1.3.0"
async "^2.0.0"
buffer-crc32 "^0.2.1"
glob "^7.0.0"
lodash "^4.8.0"
readable-stream "^2.0.0"
tar-stream "^1.5.0"
walkdir "^0.0.11"
zip-stream "^1.1.0"
async@^2.0.0, async@^2.0.1:
version "2.6.0"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4"
dependencies:
lodash "^4.14.0"
balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
bl@^1.0.0:
version "1.2.1"
resolved "https://registry.yarnpkg.com/bl/-/bl-1.2.1.tgz#cac328f7bee45730d404b692203fcb590e172d5e"
dependencies:
readable-stream "^2.0.5"
brace-expansion@^1.1.7:
version "1.1.8"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.8.tgz#c07b211c7c952ec1f8efd51a77ef0d1d3990a292"
dependencies:
balanced-match "^1.0.0"
concat-map "0.0.1"
buffer-crc32@^0.2.1:
version "0.2.13"
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
compress-commons@^1.2.0:
version "1.2.2"
resolved "https://registry.yarnpkg.com/compress-commons/-/compress-commons-1.2.2.tgz#524a9f10903f3a813389b0225d27c48bb751890f"
dependencies:
buffer-crc32 "^0.2.1"
crc32-stream "^2.0.0"
normalize-path "^2.0.0"
readable-stream "^2.0.0"
concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
crc32-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/crc32-stream/-/crc32-stream-2.0.0.tgz#e3cdd3b4df3168dd74e3de3fbbcb7b297fe908f4"
dependencies:
crc "^3.4.4"
readable-stream "^2.0.0"
crc@^3.4.4:
version "3.5.0"
resolved "https://registry.yarnpkg.com/crc/-/crc-3.5.0.tgz#98b8ba7d489665ba3979f59b21381374101a1964"
end-of-stream@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.0.tgz#7a90d833efda6cfa6eac0f4949dbb0fad3a63206"
dependencies:
once "^1.4.0"
fast-image-size@^0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/fast-image-size/-/fast-image-size-0.1.3.tgz#fca56c19f0c69cf44b15606e60d820f6863fd5b2"
fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
glob@^7.0.0:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"
graceful-fs@^4.1.0:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
dependencies:
once "^1.3.0"
wrappy "1"
inherits@2, inherits@~2.0.1, inherits@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
jszip@^2.5.0:
version "2.6.1"
resolved "https://registry.yarnpkg.com/jszip/-/jszip-2.6.1.tgz#b88f3a7b2e67a2a048152982c7a3756d9c4828f0"
dependencies:
pako "~1.0.2"
lazystream@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/lazystream/-/lazystream-1.0.0.tgz#f6995fe0f820392f61396be89462407bb77168e4"
dependencies:
readable-stream "^2.0.5"
lodash@^3.5.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
lodash@^4.14.0, lodash@^4.16.2, lodash@^4.8.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
brace-expansion "^1.1.7"
normalize-path@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9"
dependencies:
remove-trailing-separator "^1.0.1"
officegen@^0.4.5:
version "0.4.5"
resolved "https://registry.yarnpkg.com/officegen/-/officegen-0.4.5.tgz#0dc9141de29dc755e64e53a01c5d828eaf038605"
dependencies:
archiver "~1.3.0"
async "^2.0.1"
fast-image-size "^0.1.3"
jszip "^2.5.0"
lodash "^4.16.2"
readable-stream "~2.2.2"
setimmediate ">= 1.0.1"
xmlbuilder "^3.0.0"
once@^1.3.0, once@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
dependencies:
wrappy "1"
pako@~1.0.2:
version "1.0.6"
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.6.tgz#0101211baa70c4bca4a0f63f2206e97b7dfaf258"
path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
process-nextick-args@~1.0.6:
version "1.0.7"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"
readable-stream@^2.0.0, readable-stream@^2.0.5:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.3"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
safe-buffer "~5.1.1"
string_decoder "~1.0.3"
util-deprecate "~1.0.1"
readable-stream@~2.2.2:
version "2.2.11"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.11.tgz#0796b31f8d7688007ff0b93a8088d34aa17c0f72"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
safe-buffer "~5.0.1"
string_decoder "~1.0.0"
util-deprecate "~1.0.1"
remove-trailing-separator@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
safe-buffer@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7"
safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
"setimmediate@>= 1.0.1":
version "1.0.5"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"
string_decoder@~1.0.0, string_decoder@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"
dependencies:
safe-buffer "~5.1.0"
tar-stream@^1.5.0:
version "1.5.5"
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-1.5.5.tgz#5cad84779f45c83b1f2508d96b09d88c7218af55"
dependencies:
bl "^1.0.0"
end-of-stream "^1.0.0"
readable-stream "^2.0.0"
xtend "^4.0.0"
util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
walkdir@^0.0.11:
version "0.0.11"
resolved "https://registry.yarnpkg.com/walkdir/-/walkdir-0.0.11.tgz#a16d025eb931bd03b52f308caed0f40fcebe9532"
wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
xmlbuilder@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-3.1.0.tgz#2c86888f2d4eade850fa38ca7f7223f7209516e1"
dependencies:
lodash "^3.5.0"
xtend@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
zip-stream@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-1.2.0.tgz#a8bc45f4c1b49699c6b90198baacaacdbcd4ba04"
dependencies:
archiver-utils "^1.3.0"
compress-commons "^1.2.0"
lodash "^4.8.0"
readable-stream "^2.0.0"
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