Skip to content

Commit

Permalink
refactor test scripts, update README for test with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
modestysn committed Sep 25, 2021
1 parent 231cfdf commit ebdad12
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "8.9.0"
- "12.20.0"
2 changes: 1 addition & 1 deletion lib/pdfimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let PDFImage = (function() {

this.btoa = function(val) {
if (typeof window === 'undefined') {
return (new Buffer(val, 'ascii')).toString('base64');
return (new Buffer.from(val, 'ascii')).toString('base64');
}
else if (typeof window.btoa === 'function')
return window.btoa(val);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"main": "./pdfparser.js",
"scripts": {
"test": "cd ./test && sh p2j.forms.sh",
"test-misc": "node pdf2json.js -f ./test/pdf/misc/ -o ./test/target/misc/ -c -m && echo \"\nExpected: 4 success, 2 failure \" "
"test-misc": "cd ./test && sh p2j.one.sh misc . \"Expected: 4 success, 2 exception with stack trace\" "
},
"engines": {
"node": ">=12.20.0"
Expand Down
15 changes: 12 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ To Run in RESTful Web Service or as Commandline Utility

## Test

>npm i
>npm run test
After install, run command line:

Check _./test/target/_ for output JSON and test files.
> npm run test
It'll scan and parse *260* PDF AcroForm files under *_./test/pdf_*, runs with *_-s -t -c -m_* command line options, generates primary output JSON, additional text content JSON, form fields JSON and merged text JSON file for each PDF. It usually takes ~20s in my MacBook Pro to complete, check *_./test/target/_* for outputs.

### Test Exceptions

After install, run command line:

> npm run test-misc
It'll scan and parse 6 PDF files under *_./test/pdf/misc_*, also runs with *_-s -t -c -m_* command line options, generates primary output JSON, additional text content JSON, form fields JSON and merged text JSON file for 4 PDF fields, catches exceptions with stack trace, one for _unsupported encryption algorithm_, another one for _Invalid XRef stream header_.

## Code Example

Expand Down
28 changes: 14 additions & 14 deletions test/p2j.forms.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#!/usr/bin/env bash
STARTTIME=$(date +%s)
# AGENCIES=("dc" "de" "ef" "fd" "nd" "or" "pa" "sc" "va")
# for i in "${AGENCIES[@]}"
# do
# sh ./p2j.one.sh $i
# done
AGENCIES=("dc" "de" "ef" "fd" "nd" "or" "pa" "sc" "va")
for i in "${AGENCIES[@]}"
do
sh ./p2j.one.sh $i form "NO Exception, All Parsed OK"
done

# Travis CI doesn't seem to support arrays in bash for testing.
# Reverting to a bunch of commands so that build button can be shown.
sh ./p2j.one.sh dc
sh ./p2j.one.sh de
sh ./p2j.one.sh ef
sh ./p2j.one.sh fd
sh ./p2j.one.sh nd
sh ./p2j.one.sh or
sh ./p2j.one.sh pa
sh ./p2j.one.sh sc
sh ./p2j.one.sh va
# sh ./p2j.one.sh dc
# sh ./p2j.one.sh de
# sh ./p2j.one.sh ef
# sh ./p2j.one.sh fd
# sh ./p2j.one.sh nd
# sh ./p2j.one.sh or
# sh ./p2j.one.sh pa
# sh ./p2j.one.sh sc
# sh ./p2j.one.sh va

ENDTIME=$(date +%s)
echo "It takes $(($ENDTIME - $STARTTIME)) seconds to process all PDFs ..."
18 changes: 10 additions & 8 deletions test/p2j.one.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#!/usr/bin/env bash
IN_DIR_BASE=./pdf/
OUT_DIR_BASE=./target/
DATA_DIR_BASE=./data/
IN_DIR_BASE=./pdf
OUT_DIR_BASE=./target
DATA_DIR_BASE=./data
PDF2JSON=../pdf2json.js
AGENCY_NAME=$1
FORM_BASE=$2
EXPECTED_RESULT=$3

echo "-----------------------------------------------------"
echo "Clean up existing $AGENCY_NAME JSON"
echo "-----------------------------------------------------"
rm -rfv $OUT_DIR_BASE$AGENCY_NAME/
rm -rfv $OUT_DIR_BASE/$AGENCY_NAME

echo "-----------------------------------------------------"
echo "Update $AGENCY_NAME PDF"
echo "-----------------------------------------------------"
mkdir -p $OUT_DIR_BASE$AGENCY_NAME/form/
node $PDF2JSON -f $IN_DIR_BASE$AGENCY_NAME/form/ -o $OUT_DIR_BASE$AGENCY_NAME/form/ -s -t -c
diff -rq $OUT_DIR_BASE$AGENCY_NAME/form/ $DATA_DIR_BASE$AGENCY_NAME/form/
mkdir -p $OUT_DIR_BASE/$AGENCY_NAME/$FORM_BASE
node --trace-deprecation $PDF2JSON -f $IN_DIR_BASE/$AGENCY_NAME/$FORM_BASE -o $OUT_DIR_BASE/$AGENCY_NAME/$FORM_BASE -s -t -c -m
# diff -rq $OUT_DIR_BASE$AGENCY_NAME/$FORM_BASE/ $DATA_DIR_BASE$AGENCY_NAME/$FORM_BASE/

echo "-----------------------------------------------------"
echo "All JSON and PDF are updated for $AGENCY_NAME"
echo "Expected for $AGENCY_NAME : $EXPECTED_RESULT"
echo "-----------------------------------------------------"

0 comments on commit ebdad12

Please sign in to comment.