Skip to content

Commit

Permalink
cleaned code, improved docs and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Butcher <matt.butcher@microsoft.com>
  • Loading branch information
technosophos committed Jun 4, 2021
1 parent 582861a commit 31aadc7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ build:
.PHONY: test-unit
test-unit:
grain tests.gr

.PHONY: test
test:build
test: test-unit
test:
@echo EXPECT: Loading file fileserver.gr
wasmtime --dir . --env PATH_INFO=${PATH_INFO} \
--env X_MATCHED_ROUTE=${X_MATCHED_ROUTE} \
fileserver.gr.wasm > /dev/null
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ To compile:
$ make build
```

To run test (Wasmtime required):
You can run unit tests with `make test-unit` or full tests with `make test`:

```
$ make test
grain compile fileserver.gr
grain tests.gr
✅ PASS Env.splitEnvVar should parse
✅ PASS Util.reverse should reverse string
✅ PASS Util.lastIndexOf should find Some
===== Expected: =====
Some(19)
======= Got: ========
Some(18)
=====================
⛔️ FAIL UtillastIndexOf should find Some
✅ PASS Util.lastIndexOf should find None
✅ PASS Mediatype.guess should find text/plain
✅ PASS Mediatype.guess should find default type
❌ Total failed tests: 1❌
make: *** [test-unit] Error 1
```

## Running in Wagi
Expand Down
8 changes: 3 additions & 5 deletions fileserver.gr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Option from "option"
import File from "sys/file"
import String from "string"
import Mediatype from "./lib/mediatype"
import Stringutil from "./lib/stringutil"

let serve = (path) => {
File.fdWrite(File.stderr, "Fileserver: Loading file ")
Expand Down Expand Up @@ -45,11 +46,8 @@ let serve = (path) => {

let guestpath = (env) => {
let req = Option.unwrap(Map.get("PATH_INFO", env))
let mut base = Option.unwrap(Map.get("X_MATCHED_ROUTE", env))
let dots = String.indexOf("/...", base)
if (Option.isSome(dots) ) {
base = String.slice(0, Option.unwrap(dots), base)
}
let matched = Option.unwrap(Map.get("X_MATCHED_ROUTE", env))
let base = Stringutil.beforeLast("/...", matched)
String.slice(String.length(base) + 1, String.length(req), req)
}

Expand Down
20 changes: 20 additions & 0 deletions lib/stringutil.gr
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,35 @@ export let reverse = (str: String) => {
}

// Get the index of the last appearance of needle in the haystack.
//
// For a multi-character needle, this will return the end of that sequence,
// not the beginning (as indexOf does).
//
// @param needle: The string to search for
// @param haystack: The string to be searched
// @return Option<Number> The offset, if found, or a number
export let lastIndexOf = (needle: String, haystack: String) => {
let rev = reverse(haystack)
let revNeedle = reverse(needle)
let nlen = String.length(needle)
let i = String.indexOf(revNeedle, rev)
match (i) {
Some(offset) => Some(String.length(haystack) - 1 - offset),
None => None,
}
}

export let afterLast = (needle: String, haystack: String) => {
match (lastIndexOf(needle, haystack)) {
Some(index) => String.slice(index + 1, String.length(haystack), haystack),
None => haystack
}
}

export let beforeLast = (needle: String, haystack: String) => {
let nlen = String.length(needle)
match (lastIndexOf(needle, haystack)) {
Some(index) => String.slice(0, index + 1 - nlen, haystack),
None => haystack
}
}
12 changes: 11 additions & 1 deletion tests.gr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ let check = (a, b, msg: String) => {
true => Ok(String.concat("✅ PASS\t\t", msg)),
_ => {
totalErr += 1
print("===== Expected: =====")
print(a)
print("======= Got: ========")
print(b)
print("=====================")
Err(String.concat("⛔️ FAIL\t\t", msg))
}
}
Expand All @@ -35,8 +40,13 @@ let report = () => {

expect(("a", "b"), Env.splitEnvVar("a=b"), "Env.splitEnvVar should parse")
expect("gfedcba", Util.reverse("abcdefg"), "Util.reverse should reverse string")
expect(Some(5), Util.lastIndexOf("/.", "aaaa/."), "UtillastIndexOf should find Some")
expect(Some(5), Util.lastIndexOf("/.", "aaaa/."), "Util.lastIndexOf should find Some")
expect(Some(18), Util.lastIndexOf(".", "aaaa/fileserver.gr.wasm"), "Util.lastIndexOf should find last dot, not first dot")
expect(Some(12), Util.lastIndexOf(".", "/.../aaaa/..."), "Util.lastIndexOf should find last set of three dots")
expect(None, Util.lastIndexOf("??", "aaaa.."), "Util.lastIndexOf should find None")
expect("test", Util.afterLast("$.$", "foo$.$bar$.$test"), "Util.afterLast should find last match")
expect("/prefix/../path", Util.beforeLast("/..", "/prefix/../path/.."), "Util.beforeLast should return first part")
expect("/prefix/../path/..", Util.beforeLast("/$$", "/prefix/../path/.."), "Util.beforeLast should return entire string when no match")
expect("text/plain", Mediatype.guess("foo.txt"), "Mediatype.guess should find text/plain")
expect("application/octet-stream", Mediatype.guess("foo.MADEUP"), "Mediatype.guess should find default type")

Expand Down

0 comments on commit 31aadc7

Please sign in to comment.