summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2024-03-24 15:02:15 +0100
committerGitHub <noreply@github.com>2024-03-24 15:02:15 +0100
commit47c24eed0191f8f72646be63dee94ac2b35eb062 (patch)
tree808fc02c608fd56e93cd502069adcba9c561df7d
parent8fe13074a7d9fdd88c5423d1e9c5fcc4ec43a57b (diff)
downloadluasystem-47c24eed0191f8f72646be63dee94ac2b35eb062.zip
chore(test): add tests (manual) for isatty terminal function (#22)
-rw-r--r--.github/workflows/build.yml2
-rw-r--r--.luacheckrc3
-rw-r--r--doc_topics/02-development.md12
-rw-r--r--spec/03-environment_spec.lua4
-rw-r--r--spec/04-term_helper.lua13
-rw-r--r--spec/04-term_spec.lua94
-rw-r--r--spec/helpers.lua16
7 files changed, 140 insertions, 4 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index fe8295e..86b74fe 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -106,4 +106,4 @@ jobs:
- name: test
run: |
- busted --Xoutput "--color"
+ busted --exclude-tags=manual --Xoutput "--color"
diff --git a/.luacheckrc b/.luacheckrc
index 4dc4ec4..ad11fee 100644
--- a/.luacheckrc
+++ b/.luacheckrc
@@ -3,7 +3,8 @@ redefined = false
max_line_length = false
globals = {
--- "ngx",
+ "win_it",
+ "nix_it",
}
not_globals = {
diff --git a/doc_topics/02-development.md b/doc_topics/02-development.md
new file mode 100644
index 0000000..8925b83
--- /dev/null
+++ b/doc_topics/02-development.md
@@ -0,0 +1,12 @@
+# 2. Development
+
+Some tests cannot be run in CI becasue they test system specifics that
+simply do not exist in a CI environment. An example is the `isatty` function
+Which cannot be set to return a truthy value in CI.
+
+The tests concerned are all labelled with `#manual`. And in CI they will
+be skipped because `--exclude-tags=manual` is being passed to the
+`busted` command line.
+
+Hence if tests like this are being added, then please ensure the tests
+pass locally, and do not rely on CI only.
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 @@
-- Import the library that contains the environment-related functions
local system = require("system")
+require("spec.helpers")
describe("Environment Variables:", function()
@@ -11,9 +12,8 @@ describe("Environment Variables:", function()
end)
- local func = system.windows and pending or it --pending on Windows
-- Windows will unset a variable if set as an empty string
- func("should set an empty environment variable value", function()
+ nix_it("should set an empty environment variable value", function()
assert.is_true(system.setenv("TEST_VAR", ""))
assert.is_equal("", system.getenv("TEST_VAR"))
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 @@
+-- sub-script executed for isatty test
+local writefile = require("pl.utils").writefile
+local isatty = require("system").isatty
+assert(arg[1] == "--", "missing -- argument")
+local tempfile = assert(arg[2], "missing tempfile argument")
+
+-- print("my temp file: ", tempfile)
+
+assert(writefile(tempfile, [[{
+ stdin = ]]..tostring(isatty(io.stdin))..[[,
+ stdout = ]]..tostring(isatty(io.stdout))..[[,
+ stderr = ]]..tostring(isatty(io.stderr))..[[,
+}]]))
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 @@
+-- Import the library that contains the environment-related functions
+local system = require("system")
+require("spec.helpers")
+
+describe("Terminal:", function()
+
+ describe("isatty()", function()
+
+ local newtmpfile = require("pl.path").tmpname
+
+ -- set each param to true to make it a tty, to false for a stream
+ local function getttyresults(sin, sout, serr)
+ assert(type(sin) == "boolean", "sin must be a boolean")
+ assert(type(sout) == "boolean", "sout must be a boolean")
+ assert(type(serr) == "boolean", "serr must be a boolean")
+
+ local tmpfile = "./spec/04-term_helper.output"
+ local execcmd = "lua ./spec/04-term_helper.lua -- " .. tmpfile
+
+ sin = sin and "" or 'echo "hello" | '
+ if system.windows then
+ sout = sout and "" or (" > " .. newtmpfile())
+ serr = serr and "" or (" 2> " .. newtmpfile())
+ else
+ sout = sout and "" or (" > " .. newtmpfile())
+ serr = serr and "" or (" 2> " .. newtmpfile())
+ end
+
+ local cmd = sin .. execcmd .. sout .. serr
+
+ -- print("cmd: ", cmd)
+
+ os.remove(tmpfile)
+ assert(os.execute(cmd))
+ local result = assert(require("pl.utils").readfile(tmpfile))
+ os.remove(tmpfile)
+
+ -- print("result: ", result)
+
+ return assert(require("pl.compat").load("return " .. result))()
+ end
+
+
+
+ it("returns true for all if a terminal #manual", function()
+ assert.are.same(
+ {
+ stdin = true,
+ stdout = true,
+ stderr = true,
+ },
+ getttyresults(true, true, true)
+ )
+ end)
+
+
+ it("returns false for stdin if not a terminal #manual", function()
+ assert.are.same(
+ {
+ stdin = false,
+ stdout = true,
+ stderr = true,
+ },
+ getttyresults(false, true, true)
+ )
+ end)
+
+
+ it("returns false for stdout if not a terminal #manual", function()
+ assert.are.same(
+ {
+ stdin = true,
+ stdout = false,
+ stderr = true,
+ },
+ getttyresults(true, false, true)
+ )
+ end)
+
+
+ it("returns false for stderr if not a terminal #manual", function()
+ assert.are.same(
+ {
+ stdin = true,
+ stdout = true,
+ stderr = false,
+ },
+ getttyresults(true, true, false)
+ )
+ end)
+
+ end)
+
+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 @@
+local busted = require("busted")
+
+local function is_windows()
+ return package.config:sub(1,1) == "\\"
+end
+
+local function postfixer(postfix)
+ return function(description, ...)
+ return busted.pending(description.." ["..postfix.."]", ...)
+ end
+end
+
+-- win_it only executes on Windows, and is "pending" otherwise
+win_it = is_windows() and busted.it or postfixer("Windows only")
+-- nix_it only executes on Unix/Mac, and is "pending" otherwise
+nix_it = is_windows() and postfixer("Unix/Mac only") or busted.it