Skip to content

Commit

Permalink
Add Nix support to language tests (#10)
Browse files Browse the repository at this point in the history
* adapt language_tests to nix

* add nix to language test makefile

* ci: test nix on language test

* ci: install nix before test-projects

* fix outdated comment

* fix comment in nix ffi
  • Loading branch information
PgBiel authored Jul 28, 2024
1 parent 905b9e7 commit 596dd60
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ jobs:
elixir-version: "1.16.1"
rebar3-version: "3"

- name: Install Nix
uses: nixbuild/nix-quick-install-action@v27

- name: Download Glistix binary from previous job
uses: actions/download-artifact@v4
with:
Expand Down Expand Up @@ -557,6 +560,10 @@ jobs:
run: make clean bun
working-directory: ./test/language

- name: test/language Nix
run: make clean nix
working-directory: ./test/language

- name: test/compile_package0
run: make
working-directory: ./test/compile_package0
Expand Down
5 changes: 5 additions & 0 deletions test/language/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ deno:
bun:
@echo test/languate on JavaScript with Bun
cargo run --quiet -- test --target javascript --runtime bun

.phony: nix
nix:
@echo test/language on Nix
cargo run --quiet -- test --target nix
6 changes: 6 additions & 0 deletions test/language/test/ffi.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@ pub type Dynamic

@external(erlang, "ffi_erlang", "print")
@external(javascript, "./ffi_javascript.mjs", "print")
@external(nix, "./ffi_nix.nix", "print")
pub fn print(a: String) -> Nil

@external(erlang, "ffi_erlang", "append")
@external(javascript, "./ffi_javascript.mjs", "append")
@external(nix, "./ffi_nix.nix", "append")
pub fn append(a: String, b: String) -> String

@external(erlang, "ffi_erlang", "to_string")
@external(javascript, "./ffi_javascript.mjs", "toString")
@external(nix, "./ffi_nix.nix", "toString")
pub fn to_string(a: anything) -> String

@external(erlang, "ffi_erlang", "file_exists")
@external(javascript, "./ffi_javascript.mjs", "fileExists")
@external(nix, "./ffi_nix.nix", "fileExists")
pub fn file_exists(a: String) -> Bool

@external(erlang, "ffi_erlang", "halt")
@external(javascript, "./ffi_javascript.mjs", "halt")
@external(nix, "./ffi_nix.nix", "halt")
pub fn halt(a: Int) -> Nil

@external(erlang, "ffi_erlang", "to_dynamic")
@external(javascript, "./ffi_javascript.mjs", "toDynamic")
@external(nix, "./ffi_nix.nix", "toDynamic")
pub fn to_dynamic(a: x) -> Dynamic
15 changes: 15 additions & 0 deletions test/language/test/ffi_nix.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let
print = msg: builtins.trace msg null;
append = a: b: a + b;
toString = builtins.toJSON;
fileExists =
f:
let
# ./. is relative to this file in the build directory (language/build/dev/nix/ffi_nix.nix)
# In other targets, this would be relative to 'language/', so let's go back there
absPath = if builtins.isString f && builtins.substring 0 1 f != "/" then ./../../../../${f} else f;
in builtins.pathExists absPath;
halt = code: if code == 0 then null else builtins.abort (toString code);
toDynamic = x: x;
in
{ inherit print append toString fileExists halt toDynamic; }
49 changes: 49 additions & 0 deletions test/language/test/importable.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const ints_in_bit_array = <<1, 2, 3>>

pub const string_in_bit_array = <<"Gleam":utf8>>

@target(erlang)
pub const data = <<
0x1,
2,
Expand All @@ -26,6 +27,41 @@ pub const data = <<
<<<<1, 2, 3>>:bits, "Gleam":utf8, 1024>>:bits,
>>

@target(javascript)
pub const data = <<
0x1,
2,
2:size(16),
0x4:size(32),
"Gleam":utf8,
4.2:float,
<<<<1, 2, 3>>:bits, "Gleam":utf8, 1024>>:bits,
>>

@target(nix)
pub const data = <<
0x1,
2,
2:size(16),
0x4:size(32),
"Gleam":utf8,
<<<<1, 2, 3>>:bits, "Gleam":utf8, 1024>>:bits,
>>

@target(erlang)
pub fn get_bit_array() {
<<
0x1,
2,
2:size(16),
0x4:size(32),
"Gleam":utf8,
4.2:float,
<<<<1, 2, 3>>:bits, "Gleam":utf8, 1024>>:bits,
>>
}

@target(javascript)
pub fn get_bit_array() {
<<
0x1,
Expand All @@ -38,6 +74,19 @@ pub fn get_bit_array() {
>>
}

// TODO: impl :float on nix
@target(nix)
pub fn get_bit_array() {
<<
0x1,
2,
2:size(16),
0x4:size(32),
"Gleam":utf8,
<<<<1, 2, 3>>:bits, "Gleam":utf8, 1024>>:bits,
>>
}

pub const language = "gleam"

pub type Movie {
Expand Down
Loading

0 comments on commit 596dd60

Please sign in to comment.