Skip to content

Commit 2d50910

Browse files
refactor: use execSync for tests (#262)
1 parent 8f4928d commit 2d50910

11 files changed

+228
-253
lines changed

dlp/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
},
2323
"devDependencies": {
2424
"chai": "^4.2.0",
25-
"execa": "^1.0.0",
2625
"mocha": "^6.0.0",
2726
"pixelmatch": "^4.0.2",
2827
"pngjs": "^3.3.3",

dlp/risk.js

+1
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ const cli = require(`yargs`) // eslint-disable-line
792792
console.error(
793793
'Number of infoTypes and number of quasi-identifiers must be equal!'
794794
);
795+
process.exitCode = 1;
795796
} else {
796797
return kMapEstimationAnalysis(
797798
opts.callingProjectId,

dlp/system-test/deid.test.js

+41-46
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@
1818
const path = require('path');
1919
const {assert} = require('chai');
2020
const fs = require('fs');
21-
const execa = require('execa');
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2224

2325
const cmd = 'node deid.js';
24-
const exec = async cmd => {
25-
const res = await execa.shell(cmd);
26-
if (res.stderr) {
27-
throw new Error(res.stderr);
28-
}
29-
return res.stdout;
30-
};
3126
const harmfulString = 'My SSN is 372819127';
3227
const harmlessString = 'My favorite color is blue';
3328
const surrogateType = 'SSN_TOKEN';
@@ -42,110 +37,110 @@ const dateFields = 'birth_date register_date';
4237

4338
describe('deid', () => {
4439
// deidentify_masking
45-
it('should mask sensitive data in a string', async () => {
46-
const output = await exec(`${cmd} deidMask "${harmfulString}" -m x -n 5`);
47-
assert.strictEqual(output, 'My SSN is xxxxx9127');
40+
it('should mask sensitive data in a string', () => {
41+
const output = execSync(`${cmd} deidMask "${harmfulString}" -m x -n 5`);
42+
assert.include(output, 'My SSN is xxxxx9127');
4843
});
4944

50-
it('should ignore insensitive data when masking a string', async () => {
51-
const output = await exec(`${cmd} deidMask "${harmlessString}"`);
52-
assert.strictEqual(output, harmlessString);
45+
it('should ignore insensitive data when masking a string', () => {
46+
const output = execSync(`${cmd} deidMask "${harmlessString}"`);
47+
assert.include(output, harmlessString);
5348
});
5449

55-
it('should handle masking errors', async () => {
56-
const output = await exec(`${cmd} deidMask "${harmfulString}" -n -1`);
57-
assert.match(output, /Error in deidentifyWithMask/);
50+
it('should handle masking errors', () => {
51+
const output = execSync(`${cmd} deidMask "${harmfulString}" -n -1`);
52+
assert.include(output, 'Error in deidentifyWithMask');
5853
});
5954

6055
// deidentify_fpe
61-
it('should FPE encrypt sensitive data in a string', async () => {
62-
const output = await exec(
56+
it('should FPE encrypt sensitive data in a string', () => {
57+
const output = execSync(
6358
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} ${keyName} -a NUMERIC`
6459
);
6560
assert.match(output, /My SSN is \d{9}/);
66-
assert.notStrictEqual(output, harmfulString);
61+
assert.notInclude(output, harmfulString);
6762
});
6863

69-
it('should use surrogate info types in FPE encryption', async () => {
70-
const output = await exec(
64+
it('should use surrogate info types in FPE encryption', () => {
65+
const output = execSync(
7166
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} ${keyName} -a NUMERIC -s ${surrogateType}`
7267
);
7368
assert.match(output, /My SSN is SSN_TOKEN\(9\):\d{9}/);
7469
labeledFPEString = output;
7570
});
7671

77-
it('should ignore insensitive data when FPE encrypting a string', async () => {
78-
const output = await exec(
72+
it('should ignore insensitive data when FPE encrypting a string', () => {
73+
const output = execSync(
7974
`${cmd} deidFpe "${harmlessString}" ${wrappedKey} ${keyName}`
8075
);
81-
assert.strictEqual(output, harmlessString);
76+
assert.include(output, harmlessString);
8277
});
8378

84-
it('should handle FPE encryption errors', async () => {
85-
const output = await exec(
79+
it('should handle FPE encryption errors', () => {
80+
const output = execSync(
8681
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} BAD_KEY_NAME`
8782
);
8883
assert.match(output, /Error in deidentifyWithFpe/);
8984
});
9085

9186
// reidentify_fpe
92-
it('should FPE decrypt surrogate-typed sensitive data in a string', async () => {
87+
it('should FPE decrypt surrogate-typed sensitive data in a string', () => {
9388
assert.ok(labeledFPEString, 'Verify that FPE encryption succeeded.');
94-
const output = await exec(
89+
const output = execSync(
9590
`${cmd} reidFpe "${labeledFPEString}" ${surrogateType} ${wrappedKey} ${keyName} -a NUMERIC`
9691
);
97-
assert.strictEqual(output, harmfulString);
92+
assert.include(output, harmfulString);
9893
});
9994

100-
it('should handle FPE decryption errors', async () => {
101-
const output = await exec(
95+
it('should handle FPE decryption errors', () => {
96+
const output = execSync(
10297
`${cmd} reidFpe "${harmfulString}" ${surrogateType} ${wrappedKey} BAD_KEY_NAME -a NUMERIC`
10398
);
10499
assert.match(output, /Error in reidentifyWithFpe/);
105100
});
106101

107102
// deidentify_date_shift
108-
it('should date-shift a CSV file', async () => {
103+
it('should date-shift a CSV file', () => {
109104
const outputCsvFile = 'dates.actual.csv';
110-
const output = await exec(
105+
const output = execSync(
111106
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields}`
112107
);
113-
assert.match(
108+
assert.include(
114109
output,
115-
new RegExp(`Successfully saved date-shift output to ${outputCsvFile}`)
110+
`Successfully saved date-shift output to ${outputCsvFile}`
116111
);
117-
assert.notStrictEqual(
112+
assert.notInclude(
118113
fs.readFileSync(outputCsvFile).toString(),
119114
fs.readFileSync(csvFile).toString()
120115
);
121116
});
122117

123-
it('should date-shift a CSV file using a context field', async () => {
118+
it('should date-shift a CSV file using a context field', () => {
124119
const outputCsvFile = 'dates-context.actual.csv';
125120
const expectedCsvFile =
126121
'system-test/resources/date-shift-context.expected.csv';
127-
const output = await exec(
122+
const output = execSync(
128123
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName} -w ${wrappedKey}`
129124
);
130-
assert.match(
125+
assert.include(
131126
output,
132-
new RegExp(`Successfully saved date-shift output to ${outputCsvFile}`)
127+
`Successfully saved date-shift output to ${outputCsvFile}`
133128
);
134-
assert.strictEqual(
129+
assert.include(
135130
fs.readFileSync(outputCsvFile).toString(),
136131
fs.readFileSync(expectedCsvFile).toString()
137132
);
138133
});
139134

140-
it('should require all-or-none of {contextField, wrappedKey, keyName}', async () => {
141-
const output = await exec(
135+
it('should require all-or-none of {contextField, wrappedKey, keyName}', () => {
136+
const output = execSync(
142137
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName}`
143138
);
144139
assert.match(output, /You must set either ALL or NONE of/);
145140
});
146141

147-
it('should handle date-shift errors', async () => {
148-
const output = await exec(
142+
it('should handle date-shift errors', () => {
143+
const output = execSync(
149144
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount}`
150145
);
151146
assert.match(output, /Error in deidentifyWithDateShift/);

0 commit comments

Comments
 (0)