Skip to content

Commit

Permalink
parse template tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bwireman committed Jul 20, 2024
1 parent 273c8f6 commit e8b974c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/cactus/write.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const valid_hooks = [
]

@target(javascript)
pub fn tmpl() -> String {
pub fn tmpl(path: String) -> String {
let runtime =
parse_gleam_toml("./gleam.toml")
parse_gleam_toml(path)
|> try(fn(gleam_toml) {
as_invalid_field_err(tom.get_table(gleam_toml, ["javascript"]))
})
Expand All @@ -32,7 +32,7 @@ pub fn tmpl() -> String {
}

@target(erlang)
pub fn tmpl() -> String {
pub fn tmpl(_: String) -> String {
"gleam run --target erlang -m cactus -- "
}

Expand All @@ -42,6 +42,7 @@ pub fn is_valid_hook_name(name: String) -> Bool {

pub fn create_script(
hooks_dir: String,
gleam_path: String,
command: String,
) -> Result(String, CactusErr) {
print_progress("Initializing hook: " <> quote(command))
Expand All @@ -51,7 +52,10 @@ pub fn create_script(

let _ = simplifile.create_directory(hooks_dir)
let _ = simplifile.create_file(path)
use _ <- try(as_fs_err(simplifile.write(path, tmpl() <> command), path))
use _ <- try(as_fs_err(
simplifile.write(path, tmpl(gleam_path) <> command),
path,
))

simplifile.set_permissions(
path,
Expand All @@ -71,7 +75,7 @@ pub fn init(hooks_dir: String, path: String) -> Result(List(String), CactusErr)
action_body
|> dict.keys()
|> list.filter(is_valid_hook_name)
|> list.map(create_script(hooks_dir, _))
|> list.map(create_script(hooks_dir, path, _))
|> result.all
}
|> result.flatten
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/gleam/basic.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ actions = [
{ command = "B", args = ["--outdated"] },
{ command = "C", kind = "binary" },
]

[javascript]
x = true
2 changes: 2 additions & 0 deletions test/testdata/gleam/bun.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[javascript]
runtime = "bun"
2 changes: 2 additions & 0 deletions test/testdata/gleam/deno.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[javascript]
runtime = "deno"
2 changes: 2 additions & 0 deletions test/testdata/gleam/junk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[javascript]
runtime = []
2 changes: 2 additions & 0 deletions test/testdata/gleam/node.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[javascript]
runtime = "nodejs"
43 changes: 41 additions & 2 deletions test/write_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,49 @@ pub fn create_script_test() {
simplifile.delete_all([filepath.join(hook_dir, "test"), hook_dir])
|> should.be_ok

write.create_script("test/testdata/scripts", "test")
write.create_script("test/testdata/scripts", "", "test")
|> should.be_ok

simplifile.read("test/testdata/scripts/test")
|> should.be_ok
|> should.equal(write.tmpl() <> "test")
|> should.equal(write.tmpl("./gleam.toml") <> "test")
}

@target(javascript)
pub fn template_test() {
write.tmpl("./gleam.toml")
|> should.equal(
"gleam run --target javascript --runtime nodejs -m cactus -- ",
)

write.tmpl("test/testdata/gleam/basic.toml")
|> should.equal(
"gleam run --target javascript --runtime nodejs -m cactus -- ",
)

write.tmpl("test/testdata/gleam/empty.toml")
|> should.equal(
"gleam run --target javascript --runtime nodejs -m cactus -- ",
)

write.tmpl("foo/bar/baz")
|> should.equal(
"gleam run --target javascript --runtime nodejs -m cactus -- ",
)

write.tmpl("test/testdata/gleam/node.toml")
|> should.equal(
"gleam run --target javascript --runtime nodejs -m cactus -- ",
)

write.tmpl("test/testdata/gleam/bun.toml")
|> should.equal("gleam run --target javascript --runtime bun -m cactus -- ")

write.tmpl("test/testdata/gleam/deno.toml")
|> should.equal("gleam run --target javascript --runtime deno -m cactus -- ")

write.tmpl("test/testdata/gleam/junk.toml")
|> should.equal(
"gleam run --target javascript --runtime nodejs -m cactus -- ",
)
}

0 comments on commit e8b974c

Please sign in to comment.