-
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.
#29 Add unit tests to new loadConfig function
- Loading branch information
1 parent
862a8b1
commit cf3b10b
Showing
6 changed files
with
101 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Here is some garbage data | ||
It is not valid yaml | ||
{ it: is: not: even: close: to: valid: yaml } |
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,5 @@ | ||
ssh-keys: | ||
- name: no key | ||
user: demery | ||
tags: | ||
- oak |
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 @@ | ||
some_key: some_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
ssh-keys: | ||
- name: key-one | ||
key: "ssh-rsa my-key-one" | ||
user: joeblogs | ||
tags: | ||
- foo | ||
- name: pgp-yubikey | ||
key: "ssh-rsa my-key-two" | ||
user: joeblogs | ||
tags: | ||
- foo | ||
- bar |
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,79 @@ | ||
import { | ||
assertEquals, | ||
assertRejects, | ||
} from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import loadConfig from "./load_config.ts"; | ||
import { ZodError } from "../deps.ts"; | ||
|
||
Deno.test("loadConfig: must throw error if file is not found", async () => { | ||
await assertRejects( | ||
async () => { | ||
await loadConfig("non-existent-file.yaml"); | ||
}, | ||
Deno.errors.NotFound, | ||
"No such file or directory", | ||
); | ||
}); | ||
|
||
Deno.test("loadConfig: must throw syntax error if file is not valid yaml", async () => { | ||
await assertRejects( | ||
async () => { | ||
await loadConfig("./fixtures/invalid.yaml"); | ||
}, | ||
SyntaxError, | ||
"end of the stream or a document separator is expected", | ||
); | ||
}); | ||
|
||
Deno.test("loadConfig: must throw zod error if file is not valid config", async () => { | ||
await assertRejects( | ||
async () => { | ||
await loadConfig("./fixtures/missing-key.yaml"); | ||
}, | ||
ZodError, | ||
`"code": "invalid_type"`, | ||
); | ||
|
||
await assertRejects( | ||
async () => { | ||
await loadConfig("./fixtures/missing-field.yaml"); | ||
}, | ||
ZodError, | ||
`{ | ||
"code": "invalid_type", | ||
"expected": "string", | ||
"received": "undefined", | ||
"path": [ | ||
"ssh-keys", | ||
0, | ||
"key" | ||
], | ||
"message": "Required" | ||
}`, | ||
); | ||
}); | ||
|
||
Deno.test("loadConfig: must load valid config", async () => { | ||
const config = await loadConfig("./fixtures/valid.yaml"); | ||
assertEquals(config, { | ||
"ssh-keys": [ | ||
{ | ||
key: "ssh-rsa my-key-one", | ||
name: "key-one", | ||
tags: [ | ||
"foo", | ||
], | ||
user: "joeblogs", | ||
}, | ||
{ | ||
key: "ssh-rsa my-key-two", | ||
name: "pgp-yubikey", | ||
tags: [ | ||
"foo", | ||
"bar", | ||
], | ||
user: "joeblogs", | ||
}, | ||
], | ||
}); | ||
}); |