Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remino committed Jul 28, 2024
1 parent 8539bb1 commit 4ebdcf6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mkx
===

- [Formerly _mksh_](#formerly-mksh)
- [Tests](#tests)

```
mkx 2.3.0
Expand Down Expand Up @@ -29,3 +30,9 @@ OPTIONS:
As the name _mksh_ is more commonly associated with the [MirBSD Korn Shell](https://www.mirbsd.org/mksh.htm), this repo has been renamed from _mksh_ to _mkx_. The same goes for the namesake script within.

Invoking `mksh` with `mkx` used to be the equivalent of `mksh -b` or `mksh -i <interpreter>`. But the with this name change, `mkx` was now made to work like `mksh`.

## Tests

Tests are written in [Bats](https://bats-core.readthedocs.io/en/stable/).

To run them, install Bats and run `bats tests` in the project root directory.
71 changes: 71 additions & 0 deletions tests/mkx.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bats

teardown() {
[ -z "$OUTPUT_FILE" ] && return 0
[ ! -f "$OUTPUT_FILE" ] && return 0
rm -f "$OUTPUT_FILE"
}

@test "shows version" {
local version="$( grep VERSION ./mkx | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' )"

run ./mkx -v

[ "$status" -eq 0 ]
[ "$output" = "mkx $version" ]
}

@test "shows help" {
run ./mkx -h

[ "$status" -eq 0 ]
[ "${output:0:4}" = "mkx " ]
}

@test "generates script from default template" {
OUTPUT_FILE="$(mktemp)"
rm -f "$OUTPUT_FILE"
run ./mkx "$OUTPUT_FILE"

[ "$status" -eq 0 ]
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/usr/bin/env bash" ]
[ "$( wc -l < "$OUTPUT_FILE" )" -gt 2 ]
[ -x "$OUTPUT_FILE" ]
[ -f "$OUTPUT_FILE" ]
}

@test "generates script from custom template" {
OUTPUT_FILE="$(mktemp)"
rm -f "$OUTPUT_FILE"
run ./mkx -t default "$OUTPUT_FILE"

[ "$status" -eq 0 ]
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/usr/bin/env bash" ]
[ "$( wc -l < "$OUTPUT_FILE" )" -gt 2 ]
[ -x "$OUTPUT_FILE" ]
[ -f "$OUTPUT_FILE" ]
}

@test "generates bare script" {
OUTPUT_FILE="$(mktemp)"
rm -f "$OUTPUT_FILE"
run ./mkx -b "$OUTPUT_FILE"

[ "$status" -eq 0 ]
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/usr/bin/env bash" ]
[ "$( wc -l < "$OUTPUT_FILE" )" -eq 2 ]
[ -x "$OUTPUT_FILE" ]
[ -f "$OUTPUT_FILE" ]
}

@test "generates base script with /bin/sh as interpreter" {
OUTPUT_FILE="$(mktemp)"
rm -f "$OUTPUT_FILE"
run ./mkx -s "$OUTPUT_FILE"

[ "$status" -eq 0 ]
[ "$( head -n 1 "$OUTPUT_FILE" )" = "#!/bin/sh" ]
[ "$( wc -l < "$OUTPUT_FILE" )" -eq 2 ]
[ -x "$OUTPUT_FILE" ]
[ -f "$OUTPUT_FILE" ]
}

0 comments on commit 4ebdcf6

Please sign in to comment.