Skip to content

Commit

Permalink
Cleanup tests part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Rycochet committed Jan 15, 2024
1 parent 14a6bb9 commit 106ebfd
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 7 deletions.
93 changes: 93 additions & 0 deletions bin/cli.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#! /usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */

const lzString = require("../dist/index.cjs");
const fs = require("fs");
const pkg = require("../package.json");
const { Option, program } = require("commander");

function compressContent(format, content) {
switch (format) {
case "base64":
return lzString.compressToBase64(content);
case "encodeduri":
return lzString.compressToEncodedURIComponent(content);
case "raw":
default:
return lzString.compress(content);
case "uint8array":
return lzString.compressToUint8Array(content);
case "utf16":
return lzString.compressToUTF16(content);
}
}

function decompressContent(format, content) {
switch (format) {
case "base64":
return lzString.decompressFromBase64(content);
case "encodeduri":
return lzString.decompressFromEncodedURIComponent(content);
case "raw":
default:
return lzString.decompress(content);
case "uint8array":
return lzString.decompressFromUint8Array(content);
case "utf16":
return lzString.decompressFromUTF16(content);
}
}

program
.version(pkg.version)
.description("Use lz-string to compress or decompress a file")
.addOption(new Option("-d, --decompress", "if unset then this will compress"))
.addOption(
new Option("-f, --format <type>", "formatter to use")
.choices(["base64", "encodeduri", "raw", "uint8array", "utf16"])
.default("raw"),
)
.addOption(new Option("-v, --validate", "validate before returning").default(true))
.addOption(new Option("-o, --output <output-file>", "output file"))
.addOption(new Option("-q, --quiet", "don't print any error messages"))
.argument("<input-file>", "file to process")
.showHelpAfterError()
.action((file, { format, decompress, output, quiet, validate }) => {
if (!fs.existsSync(file)) {
if (!quiet) process.stderr.write(`Unable to find ${file}`);
process.exit(1);
}
try {
fs.accessSync(file, fs.constants.R_OK);
} catch {
if (!quiet) process.stderr.write(`Unable to read ${file}`);
process.exit(1);
}
const unprocessed = fs.readFileSync(file).toString();
const processed = decompress ? decompressContent(format, unprocessed) : compressContent(format, unprocessed);

if (validate) {
const validated = decompress ? compressContent(format, processed) : decompressContent(format, processed);
let valid = unprocessed.length === validated.length;

for (let i = 0; valid && i < unprocessed.length; i++) {
if (unprocessed[i] !== validated[i]) {
valid = false;
}
}
if (!valid) {
if (!quiet) process.stderr.write(`Unable to validate ${file}`);
process.exit(1);
}
}
if (processed == null) {
if (!quiet) process.stderr.write(`Unable to process ${file}`);
process.exit(1);
}
if (output) {
fs.writeFileSync(output, processed, null);
} else {
process.stdout.write(processed);
}
})
.parse();
26 changes: 20 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"compression",
"string"
],
"bin": {
"lz-string": "bin/cli.cjs"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -42,6 +45,7 @@
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"bin",
"dist"
],
"scripts": {
Expand Down Expand Up @@ -70,5 +74,8 @@
},
"override": {
"prettier": "npm:@btmills/[email protected]"
},
"dependencies": {
"commander": "11.1.0"
}
}
1 change: 0 additions & 1 deletion src/__tests__/testFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export const test_longString_fn = () => {
return testValue.join(" ");
};


/**
* This will run a series of tests against each compress / decompress pair.
*
Expand Down
72 changes: 72 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#! /usr/bin/env bash

ANSI_RESET="\033[0m"

FG_RED="\033[31m"
FG_GREEN="\033[32m"
FG_YELLOW="\033[33m"

Help() {
echo -e "\nUsage: $0 [-u]"
echo "-h: Show this help"
echo "-u: Update any failed tests"
exit
}

OPT_UPDATE=0

# ---===###===--- Code starts here ---===###===--- #

while getopts ":hu" opt; do
case ${opt} in
"h") Help ;;
"u") OPT_UPDATE=1 ; echo "Updating test files" ;;
esac
done

# file1, file2, success
compare() {
if cmp -s $1 $2; then
printf " ...${FG_GREEN}%s${ANSI_RESET}" "pass"
return 0
else
printf " ...${FG_RED}%s${ANSI_RESET}" "fail"
return 1
fi
}

# format, folder
process() {
OUTPUT=$(mktemp)
VALIDATE=$(mktemp)

printf "\nCompress: %-12s " $1
node bin/cli.cjs -v -q -f $1 testdata/$2/source.bin -o $OUTPUT
if ! compare testdata/$2/$1.bin $OUTPUT; then
node bin/cli.cjs -q -d -f $1 $OUTPUT -o $VALIDATE
if cmp -s $VALIDATE testdata/$2/source.bin; then
if [ ! -f testdata/$2/$1.bin ] || [ $OPT_UPDATE -eq 1 ]; then
cp $OUTPUT testdata/$2/$1.bin
printf " ...${FG_YELLOW}%s${ANSI_RESET}" "updated"
else
printf " ...${FG_RED}%s${ANSI_RESET}" "unsafe"
fi
else
printf " ...${FG_RED}%s${ANSI_RESET}" "validation failed"
fi
fi

printf "\nDecompress: %-12s " $1
node bin/cli.cjs -v -q -d -f $1 testdata/$2/$1.bin -o $OUTPUT
compare testdata/$2/source.bin $OUTPUT

rm $OUTPUT $VALIDATE
}

# process raw tattoo
process base64 tattoo
process encodeduri tattoo
# process uint8array tattoo
process utf16 tattoo

printf "\n\nDone\n"
1 change: 1 addition & 0 deletions testdata/tattoo/base64.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CIVwTglgdg5gBAFwIYIQezdGAaO0DWeAznlAFYCmAxghQCanqIAWFcR+0u0ECEKWOEih4AtqJBQ2YCkQAOaKEQq5hDKhQA2mklSTb6cAESikVMGjnMkMWUbii0ANzbQmCVkJlIhUBkYoUOBA5ew9XKHwAOjgAFU9Tc0trW10kMDAAT3Y0UTY0ADMWCMJ3TwAjNDpMgHISTUzRKzgoKtlccpAEHLyWIPS2AogDBgB3XmZSQiJkbLku3ApRcvo6Q2hi9k4oGPiUOrhR627TfFlN5FQMOCcIIghyzTZJNbBNjmgY4H1mNBB7tgAVQgLjA9wQtRIAEEnlQ4AAxfRnKDWUTEOBrFyaSyCHzoOQQPSaODmQJojxBUZoMD4EjlbLIMC2PiwTaJCxWGznCndawuOAyUzQQxBcLsXj5Ipiy7oNAxAByFFGDjMHJS50c/I2TCoiiIIF6YrkMlufyIDTgBJgeSgCAAtEMRiqkpzUr4GOERKIIDAwCg2GU2A0mpNWmsiIsXLaQPoLchtvBY5tqmxxh5iqIYkYAOqsES6prpQS8RBoOCaJDKMB28qVwwy66C5z6bgiI6EyaZP7sCgBirgJS4MVEPQZLBDiqaO60MGtlh3El13CjCg1fnhn1SBg+OhgEDwHkYtCyKA1brebTZPlsCRUSaFAp2xnMuAUAoFagIbD2TxEJAQOgs2zVcZBaNBumfCgWUTKBskKTZWjAUxiQ+fMtB0XAiDLLsQEORQzx7NgfGxbp4OgAoK3EARFBiABJEQCjML84FrZQGEUTZjTQDQiBIQ8VxqUCmJjS9gnuWBlzYOh8Ig5gCGKUDxm0FiiNg0gKKQKi+A4/plLUPBuipEBNG3GgRItFZfD4O1yMo0x0CyKIgA
1 change: 1 addition & 0 deletions testdata/tattoo/base64x.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CIVwTglgdg5gBAFwIYIQezdGAaO0DWeAznlAFYCmAxghQCanqIAWFcR 0u0ECEKWOEih4AtqJBQ2YCkQAOaKEQq5hDKhQA2mklSTb6cAESikVMGjnMkMWUbii0ANzbQmCVkJlIhUBkYoUOBA5ew9XKHwAOjgAFU9Tc0trW10kMDAAT3Y0UTY0ADMWCMJ3TwAjNDpMgHISTUzRKzgoKtlccpAEHLyWIPS2AogDBgB3XmZSQiJkbLku3ApRcvo6Q2hi9k4oGPiUOrhR627TfFlN5FQMOCcIIghyzTZJNbBNjmgY4H1mNBB7tgAVQgLjA9wQtRIAEEnlQ4AAxfRnKDWUTEOBrFyaSyCHzoOQQPSaODmQJojxBUZoMD4EjlbLIMC2PiwTaJCxWGznCndawuOAyUzQQxBcLsXj5Ipiy7oNAxAByFFGDjMHJS50c-I2TCoiiIIF6YrkMlufyIDTgBJgeSgCAAtEMRiqkpzUr4GOERKIIDAwCg2GU2A0mpNWmsiIsXLaQPoLchtvBY5tqmxxh5iqIYkYAOqsES6prpQS8RBoOCaJDKMB28qVwwy66C5z6bgiI6EyaZP7sCgBirgJS4MVEPQZLBDiqaO60MGtlh3El13CjCg1fnhn1SBg OhgEDwHkYtCyKA1brebTZPlsCRUSaFAp2xnMuAUAoFagIbD2TxEJAQOgs2zVcZBaNBumfCgWUTKBskKTZWjAUxiQ fMtB0XAiDLLsQEORQzx7NgfGxbp4OgAoK3EARFBiABJEQCjML84FrZQGEUTZjTQDQiBIQ8VxqUCmJjS9gnuWBlzYOh8Ig5gCGKUDxm0FiiNg0gKKQKi A4-plLUPBuipEBNG3GgRItFZfD4O1yMo0x0CyKIgAAA$$
1 change: 1 addition & 0 deletions testdata/tattoo/encodeduri.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CIVwTglgdg5gBAFwIYIQezdGAaO0DWeAznlAFYCmAxghQCanqIAWFcR+0u0ECEKWOEih4AtqJBQ2YCkQAOaKEQq5hDKhQA2mklSTb6cAESikVMGjnMkMWUbii0ANzbQmCVkJlIhUBkYoUOBA5ew9XKHwAOjgAFU9Tc0trW10kMDAAT3Y0UTY0ADMWCMJ3TwAjNDpMgHISTUzRKzgoKtlccpAEHLyWIPS2AogDBgB3XmZSQiJkbLku3ApRcvo6Q2hi9k4oGPiUOrhR627TfFlN5FQMOCcIIghyzTZJNbBNjmgY4H1mNBB7tgAVQgLjA9wQtRIAEEnlQ4AAxfRnKDWUTEOBrFyaSyCHzoOQQPSaODmQJojxBUZoMD4EjlbLIMC2PiwTaJCxWGznCndawuOAyUzQQxBcLsXj5Ipiy7oNAxAByFFGDjMHJS50c-I2TCoiiIIF6YrkMlufyIDTgBJgeSgCAAtEMRiqkpzUr4GOERKIIDAwCg2GU2A0mpNWmsiIsXLaQPoLchtvBY5tqmxxh5iqIYkYAOqsES6prpQS8RBoOCaJDKMB28qVwwy66C5z6bgiI6EyaZP7sCgBirgJS4MVEPQZLBDiqaO60MGtlh3El13CjCg1fnhn1SBg+OhgEDwHkYtCyKA1brebTZPlsCRUSaFAp2xnMuAUAoFagIbD2TxEJAQOgs2zVcZBaNBumfCgWUTKBskKTZWjAUxiQ+fMtB0XAiDLLsQEORQzx7NgfGxbp4OgAoK3EARFBiABJEQCjML84FrZQGEUTZjTQDQiBIQ8VxqUCmJjS9gnuWBlzYOh8Ig5gCGKUDxm0FiiNg0gKKQKi+A4-plLUPBuipEBNG3GgRItFZfD4O1yMo0x0CyKIgA
1 change: 1 addition & 0 deletions testdata/tattoo/source.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
During tattooing, ink is injected into the skin, initiating an immune response, and cells called "macrophages" move into the area and "eat up" the ink. The macrophages carry some of the ink to the body's lymph nodes, but some that are filled with ink stay put, embedded in the skin. That's what makes the tattoo visible under the skin. Dalhousie Uiversity's Alec Falkenham is developing a topical cream that works by targeting the macrophages that have remained at the site of the tattoo. New macrophages move in to consume the previously pigment-filled macrophages and then migrate to the lymph nodes, eventually taking all the dye with them. "When comparing it to laser-based tattoo removal, in which you see the burns, the scarring, the blisters, in this case, we've designed a drug that doesn't really have much off-target effect," he said. "We're not targeting any of the normal skin cells, so you won't see a lot of inflammation. In fact, based on the process that we're actually using, we don't think there will be any inflammation at all and it would actually be anti-inflammatory.
Binary file added testdata/tattoo/uint8array.bin
Binary file not shown.
1 change: 1 addition & 0 deletions testdata/tattoo/utf16.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ѣ尳䅌ހ猠ဥ恣Ȱ㶻冠㒖䃶㰦㨅KÆƬࡰӴ窨Ð圱緅洤с▮ऴḠ孱ၰ泠⤰“⊤ⅷᡣᔪV䵄咳㟳䀤┴䕬഼獄ᣒ䜂䗀Γ㚤悵䡬剁⠬䙈⢐ၙ㶧嗪ྠσ䀠啝✆䮋ⷎ䤬ؠԗ㇂䓸栠㌶ҁᷳ怤㍣劄ǨⒺ䳱ᖼਪ孋䝉@猒ⱡ瓖Ťá䀮瘆㊲ࢩ䣹㥎渥ᑼ彧⑖䌷奘偑碴ᵼᒚ淺㟥䪏ᅰᢐ✨ᄤ᳓⛩፻ʌ㧀㇠綆ᨨἍ䀢呀᜸ྐ↊ሠࡄ祰瀠౿⍙⃶⢸䎡噎⚲搰紈Ტϲ㒐㦰፤㱡⣭̣灄㥻ᙡ䃖ἶӺሶᖦ杘⧽㖥掠擆㐰戫䌌⼿቉䖷㨭ƨ’ੈ愃ᡙᓙ椇牖☵ࣂၠ庸坁䦎㾱ó䀩ᠾ┠†婁䒂啅᳴埠掤≱ȣƠ⡖ೆ恔䵩嗆失ଷᛲЈᜄᮏ˧ᮊ䶮ᢙ䕱ل〡櫌ࢷ⪋劢⼱͡悺ሹ⌡清▐憷⺢玿✀䑧⅒㓩缌Ԡᣋ䁪⸬⩁琹ᘨ㣊㒗⴬ඌ増ॎ巢䘴ൿ伬絲̧揁䁀㰧䣥僒ᐦ國㳺㙯䮀⑴⓰偉涬獎Àਥ㔡ۣ沾ᄩ¡桌涺圹˱傎䴘⠶⢹₌䡲㙶䘢䲂∓獍ς灂ٹ㬰࢒ᑓ揹堿ණ㪘ᴠਪ湀ѥ̰iࢠ⣬᠇Ƌ㊠愴⛬㓰ڤ⁨∂岊⠴抃☌਎Ⱜ峸ᴯ䉀猠↪⠾᧔଴⎀椡⊰ᔷ恘罬勴ḭ棉ࠩ凗ഢቍ૫焘᷎⍈榮䃒ᑤ

0 comments on commit 106ebfd

Please sign in to comment.