From 69a1d04c86dc83a5369eeb46f7cbb3272f045417 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 12 Jun 2024 17:53:23 +0200 Subject: [PATCH 01/22] feat(std): more command --- src/std/main.ab | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/std/main.ab b/src/std/main.ab index 48bb2760..234f3fbd 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -128,3 +128,56 @@ pub fun includes(arr, value) { } return false } + +pub fun move(origin: Text, destination: Text): Null { + unsafe $mv "{origin}" "{destination}"$ +} + +pub fun change_dir(path: Text): Null { + if dir_exist(origin) { + unsafe $cd "{path}" || exit$ + } + + return false +} + +pub fun make_symbolic_link(origin: Text, destination: Text): Null { + if file_exist(origin) { + unsafe $ln -s "{origin}" "{path}"$ + return true + } + + echo "The file {$origin} doesn't exist!" + return false +} + +pub fun create_dir(path: Text): Null { + if not dir_exist(origin) { + unsafe $mkdir -p "{path}"$ + } +} + +pub fun make_executable(path: Text): Null { + if file_exist(origin) { + unsafe $chmod +x "{path}"$ + return true + } + + echo "The file {$path} doesn't exist!" + return false +} + +pub fun switch_user_permission(user: Text, path: Text): Null { + // TODO when OR will be available we need to check if the folder or file exist + unsafe $chown -R "{user}" "{path}"$ +} + +pub fun delete(path: Text): Null { + if not includes(['/', '.', '..'], path) { + unsafe $rm -fr "{path}"$ + return true + } + + echo "The file {$path} can't be deleted!" + return false +} From 50fbb4eb590019989ce581fc8e2bad2a481b861f Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 19 Jun 2024 12:26:01 +0200 Subject: [PATCH 02/22] removed functions --- src/std/main.ab | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 234f3fbd..1943da69 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -129,19 +129,7 @@ pub fun includes(arr, value) { return false } -pub fun move(origin: Text, destination: Text): Null { - unsafe $mv "{origin}" "{destination}"$ -} - -pub fun change_dir(path: Text): Null { - if dir_exist(origin) { - unsafe $cd "{path}" || exit$ - } - - return false -} - -pub fun make_symbolic_link(origin: Text, destination: Text): Null { +pub fun create_symbolic_link(origin: Text, destination: Text): Bool { if file_exist(origin) { unsafe $ln -s "{origin}" "{path}"$ return true @@ -157,7 +145,7 @@ pub fun create_dir(path: Text): Null { } } -pub fun make_executable(path: Text): Null { +pub fun make_executable(path: Text): Bool { if file_exist(origin) { unsafe $chmod +x "{path}"$ return true @@ -171,13 +159,3 @@ pub fun switch_user_permission(user: Text, path: Text): Null { // TODO when OR will be available we need to check if the folder or file exist unsafe $chown -R "{user}" "{path}"$ } - -pub fun delete(path: Text): Null { - if not includes(['/', '.', '..'], path) { - unsafe $rm -fr "{path}"$ - return true - } - - echo "The file {$path} can't be deleted!" - return false -} From efdbb9f3ad55e10e6ec4871dfbdde3f2b6da0510 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 19 Jun 2024 12:40:22 +0200 Subject: [PATCH 03/22] download --- src/std/main.ab | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 1943da69..01cf6142 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -155,7 +155,25 @@ pub fun make_executable(path: Text): Bool { return false } -pub fun switch_user_permission(user: Text, path: Text): Null { - // TODO when OR will be available we need to check if the folder or file exist - unsafe $chown -R "{user}" "{path}"$ +pub fun make_executable(path: Text): Bool { + if file_exist(origin) { + unsafe $chmod +x "{path}"$ + return true + } + + echo "The file {$path} doesn't exist!" + return false +} + +pub fun switch_user_permission(user: Text, path: Text): Bool { + if file_exist(origin) or dir_exist(origin) { + unsafe $chown -R "{user}" "{path}"$ + return true + } + + return false +} + +pub fun download(url: Text, path: Text): Bool { + unsafe $wget "{url}" -P "{path}"$ } From 68ca8e64fc8619bf4e76f882a727dce5f218c369 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 19 Jun 2024 16:01:10 +0200 Subject: [PATCH 04/22] Update main.ab added is_root and is_command --- src/std/main.ab | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 01cf6142..aae4e716 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -129,6 +129,13 @@ pub fun includes(arr, value) { return false } +pub func is_command(command: Text): Bool { + if (unsafe $command -v "{command} > /dev/null) { + return true + } + return false +} + pub fun create_symbolic_link(origin: Text, destination: Text): Bool { if file_exist(origin) { unsafe $ln -s "{origin}" "{path}"$ @@ -155,16 +162,6 @@ pub fun make_executable(path: Text): Bool { return false } -pub fun make_executable(path: Text): Bool { - if file_exist(origin) { - unsafe $chmod +x "{path}"$ - return true - } - - echo "The file {$path} doesn't exist!" - return false -} - pub fun switch_user_permission(user: Text, path: Text): Bool { if file_exist(origin) or dir_exist(origin) { unsafe $chown -R "{user}" "{path}"$ @@ -175,5 +172,17 @@ pub fun switch_user_permission(user: Text, path: Text): Bool { } pub fun download(url: Text, path: Text): Bool { - unsafe $wget "{url}" -P "{path}"$ + if (is_command(''wget)) { + unsafe $wget "{url}" -P "{path}"$ + } else { + unsafe $aria2c "{url}" -d "{path}"$ + } +} + +pub fun is_root(): Bool { + id = unsafe $id -u + if (id == 0) { + return true + } + return false } From 0325817fcad43a8f5e7b13b8c21d9a6cce754362 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 19 Jun 2024 16:20:18 +0200 Subject: [PATCH 05/22] feat(download): improved fallback --- src/std/main.ab | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index aae4e716..994f5520 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -163,7 +163,7 @@ pub fun make_executable(path: Text): Bool { } pub fun switch_user_permission(user: Text, path: Text): Bool { - if file_exist(origin) or dir_exist(origin) { + if file_exist(origin) or dir_exist(origin) { unsafe $chown -R "{user}" "{path}"$ return true } @@ -172,17 +172,25 @@ pub fun switch_user_permission(user: Text, path: Text): Bool { } pub fun download(url: Text, path: Text): Bool { - if (is_command(''wget)) { + if { + not is_command("curl") { + unsafe $curl -o "{path}" "{url}"$ + } + not is_command('wget')) { unsafe $wget "{url}" -P "{path}"$ - } else { + } + not is_command('aria2c')) { unsafe $aria2c "{url}" -d "{path}"$ } + return true + } + return false } pub fun is_root(): Bool { - id = unsafe $id -u - if (id == 0) { - return true - } - return false + id = unsafe $id -u + if (id == 0) { + return true + } + return false } From bb31f54917b416e5450e9b90ce412faecedd6b56 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 19 Jun 2024 17:01:20 +0200 Subject: [PATCH 06/22] amber fix --- src/std/main.ab | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 994f5520..22bb7887 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -130,7 +130,7 @@ pub fun includes(arr, value) { } pub func is_command(command: Text): Bool { - if (unsafe $command -v "{command} > /dev/null) { + if (unsafe $command -v "{command} > /dev/null$) { return true } return false @@ -142,7 +142,7 @@ pub fun create_symbolic_link(origin: Text, destination: Text): Bool { return true } - echo "The file {$origin} doesn't exist!" + echo "The file {origin} doesn't exist!" return false } @@ -158,7 +158,7 @@ pub fun make_executable(path: Text): Bool { return true } - echo "The file {$path} doesn't exist!" + echo "The file {path} doesn't exist!" return false } @@ -188,7 +188,7 @@ pub fun download(url: Text, path: Text): Bool { } pub fun is_root(): Bool { - id = unsafe $id -u + id = unsafe $id -u$ if (id == 0) { return true } From 78c147f531c59362a112902dff7399ea15bc6c49 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 20 Jun 2024 18:53:07 +0200 Subject: [PATCH 07/22] feat(test): is_command --- src/std/main.ab | 4 ++-- src/tests/stdlib.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 22bb7887..234391bc 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -129,8 +129,8 @@ pub fun includes(arr, value) { return false } -pub func is_command(command: Text): Bool { - if (unsafe $command -v "{command} > /dev/null$) { +pub fun is_command(command: Text): Bool { + if (unsafe $command -v "{command}" > /dev/null$) { return true } return false diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 1a024c80..79ec2b14 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -418,3 +418,16 @@ fn lines() { "; test_amber!(code, "line: hello\nline: world") } + +#[test] +fn is_command() { + let code = " + import { is_command } from \"std\" + main { + if is_command(\"cat\") { + echo \"exist\" + } + } + "; + test_amber!(code, "exist") +} From c9238d79d1ee0c4b3af930af8bd2850bc18e338b Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 20 Jun 2024 19:01:41 +0200 Subject: [PATCH 08/22] feat(test): create_symbolic_link --- src/std/main.ab | 2 +- src/tests/stdlib.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/std/main.ab b/src/std/main.ab index 234391bc..db0e934a 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -138,7 +138,7 @@ pub fun is_command(command: Text): Bool { pub fun create_symbolic_link(origin: Text, destination: Text): Bool { if file_exist(origin) { - unsafe $ln -s "{origin}" "{path}"$ + unsafe $ln -s "{origin}" "{destination}"$ return true } diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 79ec2b14..e477bb0d 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -431,3 +431,22 @@ fn is_command() { "; test_amber!(code, "exist") } + + +#[test] +fn create_symbolic_link() { + let code = " + import { create_symbolic_link } from \"std\" + main { + unsafe $touch /tmp/amber-symbolic$ + if create_symbolic_link(\"/tmp/amber-symbolic\", \"/tmp/amber-symbolic-link\") { + echo \"created\" + } else { + echo \"failed\" + } + unsafe $rm /tmp/amber-symbolic$ + unsafe $rm /tmp/amber-symbolic-link$ + } + "; + test_amber!(code, "created") +} From 125c778159f3aa80af5e3e8d301bec7707701417 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 20 Jun 2024 19:08:24 +0200 Subject: [PATCH 09/22] feat(test): added 3 tests --- src/std/main.ab | 6 +++--- src/tests/stdlib.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index db0e934a..4cd0fe60 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -147,13 +147,13 @@ pub fun create_symbolic_link(origin: Text, destination: Text): Bool { } pub fun create_dir(path: Text): Null { - if not dir_exist(origin) { + if not dir_exist(path) { unsafe $mkdir -p "{path}"$ } } pub fun make_executable(path: Text): Bool { - if file_exist(origin) { + if file_exist(path) { unsafe $chmod +x "{path}"$ return true } @@ -163,7 +163,7 @@ pub fun make_executable(path: Text): Bool { } pub fun switch_user_permission(user: Text, path: Text): Bool { - if file_exist(origin) or dir_exist(origin) { + if file_exist(path) or dir_exist(path) { unsafe $chown -R "{user}" "{path}"$ return true } diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index e477bb0d..ee349b4a 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -432,7 +432,6 @@ fn is_command() { test_amber!(code, "exist") } - #[test] fn create_symbolic_link() { let code = " @@ -450,3 +449,49 @@ fn create_symbolic_link() { "; test_amber!(code, "created") } + +#[test] +fn create_dir() { + let code = " + import { create_dir, dir_exist } from \"std\" + main { + create_dir(\"/tmp/amber-test\") + if dir_exist(\"/tmp/amber-test\") { + unsafe $rm /tmp/amber-test$ + echo \"created\" + } + } + "; + test_amber!(code, "created") +} + +#[test] +fn make_executable() { + let code = " + import { make_executable } from \"std\" + main { + unsafe $touch /tmp/amber-symbolic$ + if make_executable(\"/tmp/amber-symbolic\") { + echo \"created\" + } + unsafe $rm /tmp/amber-symbolic$ + } + "; + test_amber!(code, "created") +} + +#[test] +fn switch_user_permission() { + // We use `whoami` to get the running user to assign again the same user as permission + let code = " + import { switch_user_permission } from \"std\" + main { + unsafe $touch /tmp/amber-symbolic$ + if switch_user_permission(unsafe $whoami$,\"/tmp/amber-symbolic\") { + echo \"done\" + } + unsafe $rm /tmp/amber-symbolic$ + } + "; + test_amber!(code, "done") +} From 05a9c8a8cb3c7ab8b3cfa285035742f04af25b29 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 20 Jun 2024 19:17:15 +0200 Subject: [PATCH 10/22] feat(test): added 2 tests --- src/std/main.ab | 15 +++++++++------ src/tests/stdlib.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 4cd0fe60..c1e64baf 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -176,21 +176,24 @@ pub fun download(url: Text, path: Text): Bool { not is_command("curl") { unsafe $curl -o "{path}" "{url}"$ } - not is_command('wget')) { + not is_command("wget") { unsafe $wget "{url}" -P "{path}"$ } - not is_command('aria2c')) { + not is_command("aria2c") { unsafe $aria2c "{url}" -d "{path}"$ } - return true + else { + return false + } } - return false + + return true } pub fun is_root(): Bool { - id = unsafe $id -u$ - if (id == 0) { + if (unsafe $id -u$ == "0") { return true } + return false } diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index ee349b4a..9b9e39e3 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -495,3 +495,30 @@ fn switch_user_permission() { "; test_amber!(code, "done") } + +#[test] +fn download() { + let code = " + import { download, is_command } from \"std\" + main { + if download(\"https://amber-lang.com/\", \"/tmp/amber-homepage\") { + unsafe $rm /tmp/amber-homepage$ + echo \"yes\" + } + } + "; + test_amber!(code, "yes") +} + +#[test] +fn is_root() { + let code = " + import { is_root } from \"std\" + main { + if not is_root() { + echo \"no\" + } + } + "; + test_amber!(code, "no") +} From 2cd0256aa7571d7e58ca7f6ad12eb3d42a8c17a3 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 20 Jun 2024 19:33:31 +0200 Subject: [PATCH 11/22] fix(test): download --- src/std/main.ab | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index c1e64baf..7867d0e6 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -173,13 +173,13 @@ pub fun switch_user_permission(user: Text, path: Text): Bool { pub fun download(url: Text, path: Text): Bool { if { - not is_command("curl") { + is_command("curl") { unsafe $curl -o "{path}" "{url}"$ } - not is_command("wget") { + is_command("wget") { unsafe $wget "{url}" -P "{path}"$ } - not is_command("aria2c") { + is_command("aria2c") { unsafe $aria2c "{url}" -d "{path}"$ } else { From 79d3d2859fb94a7463573a3bad31098fd5434663 Mon Sep 17 00:00:00 2001 From: b1ek Date: Fri, 21 Jun 2024 20:34:43 +1000 Subject: [PATCH 12/22] feat: spin up a small web server for download() test --- Cargo.lock | 37 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++++ src/tests/stdlib.rs | 30 +++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 334ed92e..ec3340ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,6 +12,7 @@ dependencies = [ "itertools", "similar-string", "tempfile", + "tiny_http", ] [[package]] @@ -63,6 +64,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "bitflags" version = "2.5.0" @@ -81,6 +88,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "clap" version = "4.5.4" @@ -176,6 +189,12 @@ dependencies = [ "pad", ] +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -209,6 +228,12 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + [[package]] name = "pad" version = "0.1.6" @@ -284,6 +309,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "tiny_http" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" +dependencies = [ + "ascii", + "chunked_transfer", + "httpdate", + "log", +] + [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/Cargo.toml b/Cargo.toml index c6ddb201..e84674b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,10 @@ itertools = "0.11.0" clap = { version = "4.4.18", features = ["derive"] } tempfile = "3.10.1" +# test dependencies +[dev-dependencies] +tiny_http = "0.12.0" + [profile.release] strip = true lto = true diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 9b9e39e3..f4a10e77 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -5,6 +5,7 @@ use std::fs; use std::io::Read; use std::io::Write; use std::path::PathBuf; +use std::time::Duration; use tempfile::tempdir; use tempfile::TempDir; use std::process::{Command, Stdio}; @@ -22,6 +23,16 @@ fn mkfile() -> (PathBuf, TempDir) { (file_path, temp_dir) } +fn http_server() { + use tiny_http::{Server, Response}; + + let server = Server::http("127.0.0.1:8081").expect("Can't bind to 127.0.0.1:8081"); + for req in server.incoming_requests() { + req.respond(Response::from_string("ok")).expect("Can't respond"); + break; + } +} + #[test] fn input() { let prompt_message = "Please enter your name:"; @@ -498,16 +509,25 @@ fn switch_user_permission() { #[test] fn download() { + let server = std::thread::spawn(http_server); + let code = " - import { download, is_command } from \"std\" + import { download, is_command, exit } from \"std\" main { - if download(\"https://amber-lang.com/\", \"/tmp/amber-homepage\") { - unsafe $rm /tmp/amber-homepage$ - echo \"yes\" + let tempfile = unsafe $mktemp$ + if download(\"http://127.0.0.1:8081/\", tempfile) { + $cat {tempfile}$ failed { + echo \"{tempfile} does not exist!!\" + } + unsafe $rm -f {tempfile}$ } } "; - test_amber!(code, "yes") + + test_amber!(code, "ok"); + + std::thread::sleep(Duration::from_millis(150)); + assert!(server.is_finished(), "Server has not stopped!"); } #[test] From 842c3ea2f4ab835fe87274d669ea70cf3206acb1 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 22 Jun 2024 12:12:22 +1000 Subject: [PATCH 13/22] fix: regenerate Cergo.lock --- Cargo.lock | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ca85498b..46c0e488 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,6 +81,27 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" +[[package]] +name = "assert_cmd" +version = "2.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed72493ac66d5804837f480ab3766c72bdfab91a65e565fc54fa9e42db0073a8" +dependencies = [ + "anstyle", + "bstr", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + [[package]] name = "bitflags" version = "2.5.0" @@ -277,6 +298,27 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "pad" version = "0.1.6" @@ -431,6 +473,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "termtree" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" + [[package]] name = "tiny_http" version = "0.12.0" From 7f21ea552995432c4f89d16cf2ca805bf90137fb Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 26 Jun 2024 11:49:00 +0200 Subject: [PATCH 14/22] feat(env): 3 new functions --- src/std/main.ab | 26 ++++++++++++++++++++++ src/tests/stdlib.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/std/main.ab b/src/std/main.ab index d402d001..96117d14 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -197,3 +197,29 @@ pub fun is_root(): Bool { return false } + +pub fun write(path: Text, content: Text, mode: Text): Null { + if (mode == "w") { + unsafe $echo {content} > {path}$ + } else { + unsafe $echo {content} >> {path}$ + } +} + +pub fun get_env_var(var: Text): Text { + let var = unsafe $echo "\${\!var}"$ + if var != "" { + return var + } + + if file_exist(".env") { + unsafe $source ".env"$ + return unsafe $echo "\${\!var}"$ + } + + return "" +} + +pub fun load_env_file(): Null { + unsafe $export \$("xargs < .env")$ +} diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 9a8f9705..1cc074dd 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -554,3 +554,56 @@ fn is_root() { "; test_amber!(code, "no") } + +#[test] +fn write() { + let code = " + import { write } from \"std\" + main { + let path = unsafe $mktemp -d /tmp/amber-XXXX$ + unsafe $cd {path}$ + write(\"test\", \"hello\", \"w\") + let content = unsafe $cat ./test$ + if content == \"hello\" { + echo \"yes\" + } + unsafe $rm -fr {path}$ + } + "; + test_amber!(code, "yes") +} + +#[test] +fn get_env_var() { + let code = " + import { get_env_var, write } from \"std\" + main { + let path = unsafe $mktemp -d /tmp/amber-XXXX$ + unsafe $cd {path}$ + write(\".env\", \"TEST=1\", \"w\") + if get_env_var(\"TEST\") == \"1\" { + echo \"yes\" + } + unsafe $rm -fr {path}$ + } + "; + test_amber!(code, "yes") +} + +#[test] +fn load_env_file() { + let code = " + import { load_env_file, get_env_var, write } from \"std\" + main { + let path = unsafe $mktemp -d /tmp/amber-XXXX$ + unsafe $cd {path}$ + write(\".env\", \"TEST=1\", \"w\") + load_env_file() + if get_env_var(\"TEST\") == \"1\" { + echo \"yes\" + } + unsafe $rm -fr {path}$ + } + "; + test_amber!(code, "no") +} From 4e4f2e75c00c511c129258f2bf2cf228672f38cb Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 15:12:50 +0200 Subject: [PATCH 15/22] fix(amber): now should compile --- src/std/main.ab | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 96117d14..bfce6541 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -207,14 +207,14 @@ pub fun write(path: Text, content: Text, mode: Text): Null { } pub fun get_env_var(var: Text): Text { - let var = unsafe $echo "\${\!var}"$ + let var = unsafe $echo "\$\{!var}"$ if var != "" { return var } if file_exist(".env") { unsafe $source ".env"$ - return unsafe $echo "\${\!var}"$ + return unsafe $echo "\$\{!var}"$ } return "" From 9b3ff56eda1d11d619d6960e9356fa719b10a187 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 15:19:15 +0200 Subject: [PATCH 16/22] fix(amber): now should compile --- src/std/main.ab | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index bfce6541..381d09af 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -207,9 +207,9 @@ pub fun write(path: Text, content: Text, mode: Text): Null { } pub fun get_env_var(var: Text): Text { - let var = unsafe $echo "\$\{!var}"$ - if var != "" { - return var + let _var = unsafe $echo "\$\{!var}"$ + if _var != "" { + return _var } if file_exist(".env") { From cf6bcd791feba51f9459a4b42f5ef0fef8cb1da3 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 15:24:06 +0200 Subject: [PATCH 17/22] fix(amber): clean --- src/std/main.ab | 8 -------- src/tests/stdlib.rs | 12 ++++++------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/std/main.ab b/src/std/main.ab index 381d09af..75c2a116 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -198,14 +198,6 @@ pub fun is_root(): Bool { return false } -pub fun write(path: Text, content: Text, mode: Text): Null { - if (mode == "w") { - unsafe $echo {content} > {path}$ - } else { - unsafe $echo {content} >> {path}$ - } -} - pub fun get_env_var(var: Text): Text { let _var = unsafe $echo "\$\{!var}"$ if _var != "" { diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 1cc074dd..88014531 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -558,11 +558,11 @@ fn is_root() { #[test] fn write() { let code = " - import { write } from \"std\" + import { file_write } from \"std\" main { let path = unsafe $mktemp -d /tmp/amber-XXXX$ unsafe $cd {path}$ - write(\"test\", \"hello\", \"w\") + file_write(\"test\", \"hello\") let content = unsafe $cat ./test$ if content == \"hello\" { echo \"yes\" @@ -576,11 +576,11 @@ fn write() { #[test] fn get_env_var() { let code = " - import { get_env_var, write } from \"std\" + import { get_env_var, file_write } from \"std\" main { let path = unsafe $mktemp -d /tmp/amber-XXXX$ unsafe $cd {path}$ - write(\".env\", \"TEST=1\", \"w\") + file_write(\".env\", \"TEST=1\") if get_env_var(\"TEST\") == \"1\" { echo \"yes\" } @@ -593,11 +593,11 @@ fn get_env_var() { #[test] fn load_env_file() { let code = " - import { load_env_file, get_env_var, write } from \"std\" + import { load_env_file, get_env_var, file_write } from \"std\" main { let path = unsafe $mktemp -d /tmp/amber-XXXX$ unsafe $cd {path}$ - write(\".env\", \"TEST=1\", \"w\") + file_write(\".env\", \"TEST=1\") load_env_file() if get_env_var(\"TEST\") == \"1\" { echo \"yes\" From 497c46a26edf74f671572fa6b812ed50062a993d Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 15:30:48 +0200 Subject: [PATCH 18/22] fix(test): removed write --- src/tests/stdlib.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 88014531..a52d32fd 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -555,24 +555,6 @@ fn is_root() { test_amber!(code, "no") } -#[test] -fn write() { - let code = " - import { file_write } from \"std\" - main { - let path = unsafe $mktemp -d /tmp/amber-XXXX$ - unsafe $cd {path}$ - file_write(\"test\", \"hello\") - let content = unsafe $cat ./test$ - if content == \"hello\" { - echo \"yes\" - } - unsafe $rm -fr {path}$ - } - "; - test_amber!(code, "yes") -} - #[test] fn get_env_var() { let code = " From 23262238f8737191d77b5e9675e8cda3a7de1de7 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 16:12:53 +0200 Subject: [PATCH 19/22] fix(test): maybe --- src/tests/stdlib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index a52d32fd..7dc4543e 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -562,7 +562,7 @@ fn get_env_var() { main { let path = unsafe $mktemp -d /tmp/amber-XXXX$ unsafe $cd {path}$ - file_write(\".env\", \"TEST=1\") + unsafe file_write(\".env\", \"TEST=1\") if get_env_var(\"TEST\") == \"1\" { echo \"yes\" } @@ -579,7 +579,7 @@ fn load_env_file() { main { let path = unsafe $mktemp -d /tmp/amber-XXXX$ unsafe $cd {path}$ - file_write(\".env\", \"TEST=1\") + unsafe file_write(\".env\", \"TEST=1\") load_env_file() if get_env_var(\"TEST\") == \"1\" { echo \"yes\" From d871f43625747bc9cd35bd80eb28d935836c48a6 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 16:31:15 +0200 Subject: [PATCH 20/22] fix(test): wip --- src/std/main.ab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std/main.ab b/src/std/main.ab index 75c2a116..7584f201 100644 --- a/src/std/main.ab +++ b/src/std/main.ab @@ -213,5 +213,5 @@ pub fun get_env_var(var: Text): Text { } pub fun load_env_file(): Null { - unsafe $export \$("xargs < .env")$ + unsafe $export "\$(xargs < .env)" > /dev/null$ } From f2dc1f70545cb32e1261cb79699594fc351bbd60 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 16:33:43 +0200 Subject: [PATCH 21/22] fix(test): wip --- src/tests/stdlib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index 7dc4543e..8dda45bd 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -587,5 +587,5 @@ fn load_env_file() { unsafe $rm -fr {path}$ } "; - test_amber!(code, "no") + test_amber!(code, "yes") } From 0ba31984a821091d36ff1613d1f19b36d8f5d2bc Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 1 Jul 2024 16:40:31 +0200 Subject: [PATCH 22/22] ci(cache): now will use CI cache, so it will be more fast between jobs --- .github/workflows/rust.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 65509eeb..3da14bfc 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -16,5 +16,13 @@ jobs: - uses: actions/checkout@v4 - name: Build run: cargo build --verbose + - name: Cache dependencies installed with cargo + uses: actions/cache@v3 + with: + path: | + ./target + ~/.cargo + key: rust-${{ hashFiles('Cargo.lock') }} + restore-keys: rust-${{ hashFiles('Cargo.lock') }} - name: Run all tests run: cargo test --verbose