OCaml: Add debug logging tests

This commit is contained in:
Juno Takano 2025-04-21 21:30:59 -03:00
commit f3557d5bb4
5 changed files with 39 additions and 7 deletions

View file

@ -18,7 +18,7 @@ As a program that can brick your system if something goes wrong, it's really imp
Each language will be used to implement a simple command-line interface that fulfills the specification below. "Simple" means the goal is not to cover corner cases, but to prototype and make a decision based on language syntax, ergonomics, expressiveness, documentation, ecosystem, tooling and overall experience.
Iganaq Napkin Spec v0
Iganaq Napkin Spec v0.1
A1. 'print' refers to messages for users. They MUST always be printed.
A2. 'log' refers to messages for programmers. They MUST be printed only
@ -44,7 +44,7 @@ Each language will be used to implement a simple command-line interface that ful
B2.1. version | -v | --version -> MUST print the version as in v0.8.0
B2.2. help | -h | --help -> MUST print '<long help>'
B2.3. os -> MUST print the contents of /etc/os-release
B2.3. os -> MUST print the OS name and MUST log contents of /etc/os-release
B2.4. user -> MUST print the output of the 'whoami' command
B2.5. pkg p -> MUST call the system package manager using the su_command
to install and then uninstall package p. The user MUST be able to

View file

@ -10,12 +10,15 @@
- [ ] Executability
- [-] `true` exits with status 0 (see note 3)
- [x] Add logging
- [ ] Logs only if DEBUG is set
- [x] Print each command executed, not just package names
- [x] Case with no packages provided
- [x] Prints a message
- [x] MUST NOT run any system commands
- [x] Unrecognized command: exit code 1
- [x] Command `user`: print the output of `whoami`
- [x] Command `os`: print the OS name
- [x] log the contents of /etc/os-release
- [ ] Refactorings
- [ ] Simplify and analyze `System.File`
@ -34,3 +37,7 @@
and ensure no command is ever executed without being appended to it
3. INS v0 A3.4 "running 'true' with exit code 0" requires the user to input
their password every time. This should be dropped from the spec instead
4. INS v0.1 changes requirement B2.3 to "MUST print the OS name and MUST log
contents of /etc/os-release" in order to make the logging function testable
without user input.

View file

@ -13,7 +13,7 @@ let interpret (past : Schema.schema) (input : string list) : Schema.schema =
*)
match input with
| "pkg" :: tail -> System.Package.merge past tail
| "os" :: _ -> say (System.File.read "/etc/os-release")
| "os" :: _ -> say System.Os.identify
| "user" :: _ -> say (System.Process.Reader.read [||] "whoami").output
| "echo" :: tail -> say (String.concat " " tail)
| ("version" | "-v" | "--version") :: _ ->

10
ocaml/lib/system/os.ml Normal file
View file

@ -0,0 +1,10 @@
(* the side effect could be extracted to a log list in the schema *)
let identify : string =
let os_release = String.split_on_char '\n' (File.read "/etc/os-release") in
Utilities.Log.elog (String.concat "\n" os_release);
let os_equals = List.find (String.starts_with ~prefix:"NAME=") os_release in
match String.split_on_char '=' os_equals with
| [ _; s ] ->
String.trim @@ String.map (fun c -> if c = '"' then ' ' else c) s
| _ -> "Unknown"

View file

@ -1,5 +1,13 @@
This file tests this tori implementation against the Iganaq Napkin Spec v0
A2. 'log' MUST print only if DEBUG is set and MUST be preceded by ' [log] '
$ without_debug=$(tori os 2>&1)
$ with_debug=$(DEBUG=1 tori os 2>&1)
$ test "$without_debug" != "$with_debug"
$ echo "$with_debug" | grep -Fq " [log] "
$ echo "$without_debug" | grep -Fqv " [log] "
B2.1. version | -v | --version -> MUST print the version as in v0.8.0
$ tori version
@ -22,13 +30,19 @@ B2.2. help | -h | --help -> MUST print '<long help>'
$ tori --help
<long help>
B2.3. os -> MUST print the contents of /etc/os-release
B2.3. os -> MUST print the os name
$ os_release=$(cat /etc/os-release)
$ os_name=$(cat /etc/os-release | grep '^NAME=' | cut -d = -f 2 | sed 's/"//g')
$ tori_os=$(tori os)
$ test -n "$os_release"
$ test -n "$os_name"
$ test -n "$tori_os"
$ test "$os_release" = "$tori_os"
$ test "$os_name" = "$tori_os"
B2.3. os -> MUST log the contents of /etc/os-release
$ tori_os=$(DEBUG=1 tori os 2>&1)
$ test -n "$tori_os"
$ echo "$tori_os" | grep -qFf /etc/os-release
B2.4. user -> MUST print the output of the 'whoami' command
@ -58,3 +72,4 @@ a newline, '<short help>' and exit with status code 1
Unrecognized command: unrecognized_command
<short help>
[1]