diff options
| author | ruki <[email protected]> | 2026-01-30 17:28:24 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-01-30 17:28:24 +0800 |
| commit | 51a68ee33918b95811420beb6a56673a83abe6cc (patch) | |
| tree | aba2c0591c0285709eede0e6adf4f5a68b066674 /tests | |
| parent | 45f856e3ccdec7da98ac7f07251f30d85640d268 (diff) | |
| parent | d309d7832a6b079d81c10f2f6449081a1d7881d2 (diff) | |
Merge pull request #7278 from xmake-io/isexec
Improve os.isexec
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modules/binutils/test.lua | 78 | ||||
| -rw-r--r-- | tests/modules/os/test.lua | 39 |
2 files changed, 117 insertions, 0 deletions
diff --git a/tests/modules/binutils/test.lua b/tests/modules/binutils/test.lua new file mode 100644 index 000000000..bbb466144 --- /dev/null +++ b/tests/modules/binutils/test.lua @@ -0,0 +1,78 @@ +import("core.base.binutils") + +function test_format(t) + local tempdir = "temp/binutils_format" + os.tryrm(tempdir) + os.mkdir(tempdir) + + local unknown = path.join(tempdir, "unknown.bin") + io.writefile(unknown, "12345678") + local format = binutils.format(unknown) + t:are_equal(format, "unknown") + + local scriptfile = path.join(tempdir, "a.sh") + io.writefile(scriptfile, "#!/bin/sh\nexit 0\n") + t:are_equal(binutils.format(scriptfile), "shebang") + + local apefile = path.join(tempdir, "a.ape") + io.writefile(apefile, "MZqFpD00") + t:are_equal(binutils.format(apefile), "ape") + + local apecom = path.join(tempdir, "a.com") + io.writefile(apecom, "MZqFpD00") + t:are_equal(binutils.format(apecom), "ape") + + local comfile = path.join(tempdir, "b.com") + io.writefile(comfile, "12345678") + t:are_equal(binutils.format(comfile), "unknown") + + local programfile = os.programfile() + if programfile then + local expected = "elf" + if is_host("windows") then + expected = "pe" + elseif is_host("macosx", "iphoneos", "watchos", "appletvos") then + expected = "macho" + end + local format = binutils.format(programfile) + if format ~= "ape" then + t:are_equal(format, expected) + end + end + + local ar = path.join(tempdir, "a.a") + io.writefile(ar, "!<arch>\n") + t:are_equal(binutils.format(ar), "ar") + + local elf = path.join(tempdir, "a.elf") + io.writefile(elf, string.char(0x7f, string.byte("E"), string.byte("L"), string.byte("F"), 0, 0, 0, 0)) + t:are_equal(binutils.format(elf), "elf") + + local macho = path.join(tempdir, "a.macho") + io.writefile(macho, string.char(0xfe, 0xed, 0xfa, 0xcf, 0, 0, 0, 0)) + t:are_equal(binutils.format(macho), "macho") + + local coff = path.join(tempdir, "a.obj") + io.writefile(coff, string.char(0x4c, 0x01, 0, 0, 0, 0, 0, 0)) + t:are_equal(binutils.format(coff), "coff") + + local pefile = path.join(tempdir, "a.exe") + local pe = {} + for _ = 1, 0x44 do + table.insert(pe, 0) + end + pe[1] = string.byte("M") + pe[2] = string.byte("Z") + pe[0x3c + 1] = 0x40 + pe[0x3c + 2] = 0 + pe[0x3c + 3] = 0 + pe[0x3c + 4] = 0 + pe[0x40 + 1] = string.byte("P") + pe[0x40 + 2] = string.byte("E") + pe[0x40 + 3] = 0 + pe[0x40 + 4] = 0 + io.writefile(pefile, string.char(table.unpack(pe))) + t:are_equal(binutils.format(pefile), "pe") + + os.tryrm(tempdir) +end diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index 2e04e1b35..2dcab35c6 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -169,3 +169,42 @@ function test_async(t) t:require(os.isdir(tmpdir2)) os.rm(tmpdir2, {async = true, detach = true}) end + +function test_isexec(t) + local tempdir = "temp/isexec" + os.tryrm(tempdir) + os.mkdir(tempdir) + + local programfile = os.programfile() + if programfile then + t:require(os.isexec(programfile)) + end + + local filepath = path.join(tempdir, "script") + io.writefile(filepath, "echo test\n") + + if is_host("windows") then + local batfile = path.join(tempdir, "a.bat") + io.writefile(batfile, "echo test\r\n") + t:require(os.isexec(batfile)) + + local comfile = path.join(tempdir, "a.com") + io.writefile(comfile, "12345678") + t:require(os.isexec(comfile)) + + local suffix = path.join(tempdir, "prog") + io.writefile(suffix .. ".exe", "") + t:require(os.isexec(suffix)) + + local suffix2 = path.join(tempdir, "prog2") + io.writefile(suffix2 .. ".com", "") + t:require(os.isexec(suffix2)) + else + os.vrunv("chmod", {"-x", filepath}) + t:require_not(os.isexec(filepath)) + os.vrunv("chmod", {"+x", filepath}) + t:require(os.isexec(filepath)) + end + + os.tryrm(tempdir) +end |
