jm

Odin for scripts: small packages and a runner, on core: only
Log | Files | Refs | README

sh_test.odin (1773B)


      1 package sh
      2 
      3 import "core:strings"
      4 import "core:testing"
      5 
      6 @(test)
      7 which_finds_shell :: proc(t: ^testing.T) {
      8 	name := "cmd" when ODIN_OS == .Windows else "sh"
      9 	p, found := which(name, context.temp_allocator)
     10 	testing.expect(t, found, "shell must be on PATH")
     11 	testing.expect(t, len(p) > 0)
     12 	_, found = which("definitely-not-a-program-3f9a", context.temp_allocator)
     13 	testing.expect(t, !found)
     14 }
     15 
     16 @(test)
     17 out_and_lines :: proc(t: ^testing.T) {
     18 	s, ok := out("echo hello", allocator = context.temp_allocator)
     19 	testing.expect(t, ok)
     20 	testing.expect_value(t, s, "hello")
     21 
     22 	ls, lok := lines("echo one&& echo two", allocator = context.temp_allocator)
     23 	testing.expect(t, lok)
     24 	testing.expect_value(t, len(ls), 2)
     25 	if len(ls) == 2 {
     26 		testing.expect_value(t, ls[0], "one")
     27 		testing.expect_value(t, ls[1], "two")
     28 	}
     29 }
     30 
     31 @(test)
     32 failure_is_reported :: proc(t: ^testing.T) {
     33 	r := capture("exit 3", allocator = context.temp_allocator)
     34 	testing.expect(t, !r.ok)
     35 	testing.expect_value(t, r.code, 3)
     36 	msg := error(r, context.temp_allocator)
     37 	testing.expect(t, strings.has_prefix(msg, "command failed (exit 3): exit 3"), msg)
     38 
     39 	r = exec({"definitely-not-a-program-3f9a"}, allocator = context.temp_allocator)
     40 	testing.expect(t, !r.ok)
     41 	testing.expect(t, r.err != nil, "starting a missing program must set err")
     42 }
     43 
     44 @(test)
     45 split_lines_strips_endings :: proc(t: ^testing.T) {
     46 	// Trailing empty lines are dropped; interior ones stay.
     47 	ls := split_lines("a\r\nb\n\n", context.temp_allocator)
     48 	testing.expect_value(t, len(ls), 2)
     49 	if len(ls) == 2 {
     50 		testing.expect_value(t, ls[0], "a")
     51 		testing.expect_value(t, ls[1], "b")
     52 	}
     53 	ls = split_lines("a\n\nb\n", context.temp_allocator)
     54 	testing.expect_value(t, len(ls), 3)
     55 	testing.expect_value(t, len(split_lines("", context.temp_allocator)), 0)
     56 }