Skip to content

Commit

Permalink
#29 Add unit tests to new loadConfig function
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Aug 4, 2024
1 parent 862a8b1 commit cf3b10b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ deno run --env --allow-net --allow-env --allow-read main.ts
### Run tests

```sh
deno test
deno test --allow-read
```

### Local Helm Chart
Expand Down
3 changes: 3 additions & 0 deletions fixtures/invalid.yaml
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 }
5 changes: 5 additions & 0 deletions fixtures/missing-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ssh-keys:
- name: no key
user: demery
tags:
- oak
1 change: 1 addition & 0 deletions fixtures/missing-key.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some_key: some_value
12 changes: 12 additions & 0 deletions fixtures/valid.yaml
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
79 changes: 79 additions & 0 deletions src/load_config.test.ts
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",
},
],
});
});

0 comments on commit cf3b10b

Please sign in to comment.