diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-03-24 15:02:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-24 15:02:15 +0100 |
| commit | 47c24eed0191f8f72646be63dee94ac2b35eb062 (patch) | |
| tree | 808fc02c608fd56e93cd502069adcba9c561df7d /spec | |
| parent | 8fe13074a7d9fdd88c5423d1e9c5fcc4ec43a57b (diff) | |
| download | luasystem-47c24eed0191f8f72646be63dee94ac2b35eb062.tar.gz luasystem-47c24eed0191f8f72646be63dee94ac2b35eb062.tar.bz2 luasystem-47c24eed0191f8f72646be63dee94ac2b35eb062.zip | |
chore(test): add tests (manual) for isatty terminal function (#22)
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/03-environment_spec.lua | 4 | ||||
| -rw-r--r-- | spec/04-term_helper.lua | 13 | ||||
| -rw-r--r-- | spec/04-term_spec.lua | 94 | ||||
| -rw-r--r-- | spec/helpers.lua | 16 |
4 files changed, 125 insertions, 2 deletions
diff --git a/spec/03-environment_spec.lua b/spec/03-environment_spec.lua index 842ed6f..96f4a78 100644 --- a/spec/03-environment_spec.lua +++ b/spec/03-environment_spec.lua | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | -- Import the library that contains the environment-related functions | 1 | -- Import the library that contains the environment-related functions |
| 2 | local system = require("system") | 2 | local system = require("system") |
| 3 | require("spec.helpers") | ||
| 3 | 4 | ||
| 4 | describe("Environment Variables:", function() | 5 | describe("Environment Variables:", function() |
| 5 | 6 | ||
| @@ -11,9 +12,8 @@ describe("Environment Variables:", function() | |||
| 11 | end) | 12 | end) |
| 12 | 13 | ||
| 13 | 14 | ||
| 14 | local func = system.windows and pending or it --pending on Windows | ||
| 15 | -- Windows will unset a variable if set as an empty string | 15 | -- Windows will unset a variable if set as an empty string |
| 16 | func("should set an empty environment variable value", function() | 16 | nix_it("should set an empty environment variable value", function() |
| 17 | assert.is_true(system.setenv("TEST_VAR", "")) | 17 | assert.is_true(system.setenv("TEST_VAR", "")) |
| 18 | assert.is_equal("", system.getenv("TEST_VAR")) | 18 | assert.is_equal("", system.getenv("TEST_VAR")) |
| 19 | end) | 19 | end) |
diff --git a/spec/04-term_helper.lua b/spec/04-term_helper.lua new file mode 100644 index 0000000..7fd39d2 --- /dev/null +++ b/spec/04-term_helper.lua | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | -- sub-script executed for isatty test | ||
| 2 | local writefile = require("pl.utils").writefile | ||
| 3 | local isatty = require("system").isatty | ||
| 4 | assert(arg[1] == "--", "missing -- argument") | ||
| 5 | local tempfile = assert(arg[2], "missing tempfile argument") | ||
| 6 | |||
| 7 | -- print("my temp file: ", tempfile) | ||
| 8 | |||
| 9 | assert(writefile(tempfile, [[{ | ||
| 10 | stdin = ]]..tostring(isatty(io.stdin))..[[, | ||
| 11 | stdout = ]]..tostring(isatty(io.stdout))..[[, | ||
| 12 | stderr = ]]..tostring(isatty(io.stderr))..[[, | ||
| 13 | }]])) | ||
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua new file mode 100644 index 0000000..a2034aa --- /dev/null +++ b/spec/04-term_spec.lua | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | -- Import the library that contains the environment-related functions | ||
| 2 | local system = require("system") | ||
| 3 | require("spec.helpers") | ||
| 4 | |||
| 5 | describe("Terminal:", function() | ||
| 6 | |||
| 7 | describe("isatty()", function() | ||
| 8 | |||
| 9 | local newtmpfile = require("pl.path").tmpname | ||
| 10 | |||
| 11 | -- set each param to true to make it a tty, to false for a stream | ||
| 12 | local function getttyresults(sin, sout, serr) | ||
| 13 | assert(type(sin) == "boolean", "sin must be a boolean") | ||
| 14 | assert(type(sout) == "boolean", "sout must be a boolean") | ||
| 15 | assert(type(serr) == "boolean", "serr must be a boolean") | ||
| 16 | |||
| 17 | local tmpfile = "./spec/04-term_helper.output" | ||
| 18 | local execcmd = "lua ./spec/04-term_helper.lua -- " .. tmpfile | ||
| 19 | |||
| 20 | sin = sin and "" or 'echo "hello" | ' | ||
| 21 | if system.windows then | ||
| 22 | sout = sout and "" or (" > " .. newtmpfile()) | ||
| 23 | serr = serr and "" or (" 2> " .. newtmpfile()) | ||
| 24 | else | ||
| 25 | sout = sout and "" or (" > " .. newtmpfile()) | ||
| 26 | serr = serr and "" or (" 2> " .. newtmpfile()) | ||
| 27 | end | ||
| 28 | |||
| 29 | local cmd = sin .. execcmd .. sout .. serr | ||
| 30 | |||
| 31 | -- print("cmd: ", cmd) | ||
| 32 | |||
| 33 | os.remove(tmpfile) | ||
| 34 | assert(os.execute(cmd)) | ||
| 35 | local result = assert(require("pl.utils").readfile(tmpfile)) | ||
| 36 | os.remove(tmpfile) | ||
| 37 | |||
| 38 | -- print("result: ", result) | ||
| 39 | |||
| 40 | return assert(require("pl.compat").load("return " .. result))() | ||
| 41 | end | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | it("returns true for all if a terminal #manual", function() | ||
| 46 | assert.are.same( | ||
| 47 | { | ||
| 48 | stdin = true, | ||
| 49 | stdout = true, | ||
| 50 | stderr = true, | ||
| 51 | }, | ||
| 52 | getttyresults(true, true, true) | ||
| 53 | ) | ||
| 54 | end) | ||
| 55 | |||
| 56 | |||
| 57 | it("returns false for stdin if not a terminal #manual", function() | ||
| 58 | assert.are.same( | ||
| 59 | { | ||
| 60 | stdin = false, | ||
| 61 | stdout = true, | ||
| 62 | stderr = true, | ||
| 63 | }, | ||
| 64 | getttyresults(false, true, true) | ||
| 65 | ) | ||
| 66 | end) | ||
| 67 | |||
| 68 | |||
| 69 | it("returns false for stdout if not a terminal #manual", function() | ||
| 70 | assert.are.same( | ||
| 71 | { | ||
| 72 | stdin = true, | ||
| 73 | stdout = false, | ||
| 74 | stderr = true, | ||
| 75 | }, | ||
| 76 | getttyresults(true, false, true) | ||
| 77 | ) | ||
| 78 | end) | ||
| 79 | |||
| 80 | |||
| 81 | it("returns false for stderr if not a terminal #manual", function() | ||
| 82 | assert.are.same( | ||
| 83 | { | ||
| 84 | stdin = true, | ||
| 85 | stdout = true, | ||
| 86 | stderr = false, | ||
| 87 | }, | ||
| 88 | getttyresults(true, true, false) | ||
| 89 | ) | ||
| 90 | end) | ||
| 91 | |||
| 92 | end) | ||
| 93 | |||
| 94 | end) | ||
diff --git a/spec/helpers.lua b/spec/helpers.lua new file mode 100644 index 0000000..8f766e7 --- /dev/null +++ b/spec/helpers.lua | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | local busted = require("busted") | ||
| 2 | |||
| 3 | local function is_windows() | ||
| 4 | return package.config:sub(1,1) == "\\" | ||
| 5 | end | ||
| 6 | |||
| 7 | local function postfixer(postfix) | ||
| 8 | return function(description, ...) | ||
| 9 | return busted.pending(description.." ["..postfix.."]", ...) | ||
| 10 | end | ||
| 11 | end | ||
| 12 | |||
| 13 | -- win_it only executes on Windows, and is "pending" otherwise | ||
| 14 | win_it = is_windows() and busted.it or postfixer("Windows only") | ||
| 15 | -- nix_it only executes on Unix/Mac, and is "pending" otherwise | ||
| 16 | nix_it = is_windows() and postfixer("Unix/Mac only") or busted.it | ||
