-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(run): Allow zip path as first argument (#3)
...to allow usage with other zip files. - exposed formerly internal methods `contentLoader` and `entriesLoader` - both of them now use a (zip file) location based in memory `cache` - `xmltest.json` is now only required as part of `getEntries`, not when the module is loaded. It's also the only method that uses those entries. - tests for `run` method
- Loading branch information
Showing
9 changed files
with
232 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
exports.cache = () => { | ||
let map = new Map(); | ||
|
||
return { | ||
clear: () => { | ||
map = new Map(); | ||
}, | ||
delete: (key) => map.delete(key), | ||
get: (key) => map.get(key), | ||
has: (key) => map.has(key), | ||
keys: () => [...map.keys()], | ||
set: (key, value) => { | ||
map.set(key, value); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
# Creates test.zip file in the same directory | ||
|
||
# clear the folder if it already existed | ||
rm -rf data/folder | ||
# the `data` directory is .gitignored, so we can use it as temp | ||
mkdir -p data/folder | ||
# add one file that has content | ||
echo CONTENT > data/folder/file.ext | ||
# and one empty file | ||
touch data/folder/empty | ||
|
||
# It is important for the tests to have the optional `folder` entry in the zip file. | ||
(cd data && zip ../test.zip folder folder/*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { cache } = require("../cache"); | ||
|
||
describe("cache", () => { | ||
test("should have no keys initially", () => { | ||
expect(cache().keys()).toHaveLength(0); | ||
}); | ||
test.each(["key", 0, 1, "", null, NaN, undefined])( | ||
"should store value for key `%s`", | ||
(key) => { | ||
const value = {}; | ||
const it = cache(); | ||
|
||
expect(it.has(key)).toBe(false); | ||
it.set(key, value); | ||
expect(it.has(key)).toBe(true); | ||
expect(it.keys()).toHaveLength(1); | ||
expect(it.get(key)).toBe(value); | ||
} | ||
); | ||
test.each(["key", 0, 1, "", null, NaN, undefined])( | ||
"should return undefined for key `%s`", | ||
(key) => { | ||
const it = cache(); | ||
|
||
expect(it.has(key)).toBe(false); | ||
expect(it.get(key)).toBeUndefined(); | ||
} | ||
); | ||
test.each(["key", 0, 1, "", null, NaN, undefined])( | ||
"should delete key for key `%s`", | ||
(key) => { | ||
const it = cache(); | ||
it.set(key, {}); | ||
it.delete(key); | ||
expect(it.has(key)).toBe(false); | ||
expect(it.get(key)).toBeUndefined(); | ||
} | ||
); | ||
test("should clear the cache", () => { | ||
const it = cache(); | ||
it.set("key", {}); | ||
it.clear(); | ||
expect(it.has("key")).toBe(false); | ||
expect(it.keys()).toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const entries = require("../xmltest.json"); | ||
const { run, contentLoader, entriesLoader } = require("../xmltest.js"); | ||
const path = require("path"); | ||
|
||
const README_PATH = "xmltest/readme.html"; | ||
const TEST_ZIP_PATH = path.join(__dirname, "..", "test.zip"); | ||
|
||
const TEST_ZIP_ENTRIES = { | ||
"folder/": "", | ||
"folder/file.ext": "file.ext", | ||
"folder/empty": "empty", | ||
}; | ||
|
||
describe("run", () => { | ||
beforeEach(contentLoader.CACHE.clear); | ||
beforeEach(entriesLoader.CACHE.clear); | ||
describe("only filter arguments", () => { | ||
test("should return entries without any arguments", async () => { | ||
// FYI: xmltest.zip doesn't contain any folder entries | ||
expect(await run()).toEqual(entries); | ||
expect(contentLoader.CACHE.keys()).toHaveLength(0); | ||
expect(entriesLoader.CACHE.keys()).toHaveLength(1); | ||
}); | ||
test("should return all (file) keys in entries with first argument 'xmltest'", async () => { | ||
expect(Object.keys(await run("xmltest"))).toEqual(Object.keys(entries)); | ||
expect(contentLoader.CACHE.keys()).toHaveLength(1); | ||
expect(entriesLoader.CACHE.keys()).toHaveLength(0); | ||
}); | ||
test("should return the content of readme.html with first argument 'xmltest/readme.html'", async () => { | ||
expect(await run(README_PATH)).toMatch(/^<HTML>.*/); | ||
}); | ||
test("should return dict with single key when multiple filters only match one entry", async () => { | ||
const actual = await run(...README_PATH.split("/")); | ||
expect(Object.keys(actual)).toHaveLength(1); | ||
expect(actual[README_PATH]).toMatch(/^<HTML>.*/); | ||
}); | ||
}); | ||
describe("first argument is path to zip", () => { | ||
test.each(["./test.zip", "../xmltest/test.zip", TEST_ZIP_PATH])( | ||
"should return all entries without any filter arguments %s", | ||
async (pathToZip) => { | ||
expect(await run(pathToZip)).toEqual(TEST_ZIP_ENTRIES); | ||
} | ||
); | ||
test("should return all file keys in entries with first filter argument 'folder'", async () => { | ||
const actual = await run(TEST_ZIP_PATH, "folder"); | ||
expect(Object.keys(actual)).toEqual( | ||
Object.keys(TEST_ZIP_ENTRIES).filter((entry) => !entry.endsWith("/")) | ||
); | ||
}); | ||
test("should return the content when first filter argument matches a file", async () => { | ||
expect(await run(TEST_ZIP_PATH, "folder/file.ext")).toBe("CONTENT\n"); | ||
expect(await run(TEST_ZIP_PATH, "folder/empty")).toBe(""); | ||
}); | ||
test("should return dict with single key when multiple filters only match one entry", async () => { | ||
const actual = await run(TEST_ZIP_PATH, "folder", "file"); | ||
expect(Object.keys(actual)).toHaveLength(1); | ||
expect(actual["folder/file.ext"]).toMatch("CONTENT\n"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.