From 32b4f3d65d801284bf3db329a271ae482a82bac2 Mon Sep 17 00:00:00 2001 From: Thijs Date: Sun, 24 Mar 2024 14:02:07 +0100 Subject: [PATCH] chore(test): add tests for isatty terminal function --- spec/04-term_helper.lua | 13 ++++++ spec/04-term_spec.lua | 94 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 spec/04-term_helper.lua create mode 100644 spec/04-term_spec.lua 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..86ea37d --- /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(load("return " .. result))() + end + + + + it("returns true for all if a terminal", function() + assert.are.same( + { + stdin = true, + stdout = true, + stderr = true, + }, + getttyresults(true, true, true) + ) + end) + + + it("returns false for stdin if not a terminal", function() + assert.are.same( + { + stdin = false, + stdout = true, + stderr = true, + }, + getttyresults(false, true, true) + ) + end) + + + it("returns false for stdout if not a terminal", function() + assert.are.same( + { + stdin = true, + stdout = false, + stderr = true, + }, + getttyresults(true, false, true) + ) + end) + + + it("returns false for stderr if not a terminal", function() + assert.are.same( + { + stdin = true, + stdout = true, + stderr = false, + }, + getttyresults(true, true, false) + ) + end) + + end) + +end)