|
| 1 | +-- |
| 2 | +-- Tests for the system module |
| 3 | +-- |
| 4 | +local system = require 'system' |
| 5 | +local tasty = require 'tasty' |
| 6 | + |
| 7 | +local group = tasty.test_group |
| 8 | +local test = tasty.test_case |
| 9 | +local assert = tasty.assert |
| 10 | + |
| 11 | +--- helper function, combining with_wd and with_tmpdir |
| 12 | +function in_tmpdir (callback) |
| 13 | + return function () |
| 14 | + system.with_tmpdir('test-sytem-tmpdir', function (tmpdir) |
| 15 | + system.with_wd(tmpdir, callback) |
| 16 | + end) |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +--- dummy string for testing |
| 21 | +local token = 'Banana' |
| 22 | + |
| 23 | +--- check if token can be written into a file in given directory; returns the |
| 24 | +--- content of this file. |
| 25 | +function write_read_token (dir, filename) |
| 26 | + local filename = string.format('%s/%s', dir, 'foo.txt') |
| 27 | + local fh = io.open(filename, 'w') |
| 28 | + fh:write(token .. '\n') |
| 29 | + fh:close() |
| 30 | + return io.open(filename):read '*l' |
| 31 | +end |
| 32 | + |
| 33 | + |
| 34 | +-- Check existence static fields |
| 35 | +return { |
| 36 | + group 'static fields' { |
| 37 | + test('arch', function () |
| 38 | + assert.are_equal(type(system.arch), 'string') |
| 39 | + end), |
| 40 | + test('compiler_name', function () |
| 41 | + assert.are_equal(type(system.compiler_name), 'string') |
| 42 | + end), |
| 43 | + test('compiler_version', function () |
| 44 | + assert.are_equal(type(system.compiler_version), 'table') |
| 45 | + end), |
| 46 | + test('os', function () |
| 47 | + assert.are_equal(type(system.os), 'string') |
| 48 | + end), |
| 49 | + }, |
| 50 | + |
| 51 | + group 'environment' { |
| 52 | + test('getenv returns same result as os.getenv', function () |
| 53 | + assert.are_equal(system.getenv 'PATH', os.getenv 'PATH') |
| 54 | + end), |
| 55 | + |
| 56 | + test('setenv sets environment values', function () |
| 57 | + system.setenv('HSLUA_SYSTEM_MODULE', 'test') |
| 58 | + assert.are_equal(os.getenv 'HSLUA_SYSTEM_MODULE', 'test') |
| 59 | + end), |
| 60 | + |
| 61 | + }, |
| 62 | + group 'getwd' { |
| 63 | + test('returns a string', function () |
| 64 | + assert.are_equal(type(system.getwd()), 'string') |
| 65 | + end) |
| 66 | + }, |
| 67 | + |
| 68 | + group 'env' { |
| 69 | + test('returns a table', function () |
| 70 | + assert.are_equal(type(system.env()), 'table') |
| 71 | + end) |
| 72 | + }, |
| 73 | + |
| 74 | + group 'ls' { |
| 75 | + test('returns a table', function () |
| 76 | + assert.are_equal(type(system.ls('.')), 'table') |
| 77 | + end), |
| 78 | + test('lists files in directory', in_tmpdir(function () |
| 79 | + io.open('README.org', 'w'):close() |
| 80 | + assert.are_same(system.ls '.', {'README.org'}) |
| 81 | + end)), |
| 82 | + test('argument defaults to `.`', function () |
| 83 | + assert.are_equal(#system.ls('.'), #system.ls()) |
| 84 | + end), |
| 85 | + test('fails when arg is not a directory', function () |
| 86 | + assert.error_matches( |
| 87 | + function () system.ls('thisdoesnotexist') end, |
| 88 | + 'thisdoesnotexist' |
| 89 | + ) |
| 90 | + assert.error_matches( |
| 91 | + function () system.ls('README.md') end, |
| 92 | + 'README%.md' |
| 93 | + ) |
| 94 | + end) |
| 95 | + }, |
| 96 | + |
| 97 | + group 'mkdir' { |
| 98 | + test('create directory', in_tmpdir(function () |
| 99 | + system.mkdir 'foo' |
| 100 | + assert.are_equal((system.ls())[1], 'foo') |
| 101 | + end)), |
| 102 | + test('create nested directories', in_tmpdir(function () |
| 103 | + system.mkdir('foo/bar', true) |
| 104 | + assert.are_equal((system.ls())[1], 'foo') |
| 105 | + assert.are_equal((system.ls 'foo')[1], 'bar') |
| 106 | + end)), |
| 107 | + test('cannot create existing directory', in_tmpdir(function () |
| 108 | + assert.error_matches(function () system.mkdir '.' end, '%.') |
| 109 | + end)), |
| 110 | + test('optionally ignores existing directories', in_tmpdir(function () |
| 111 | + system.mkdir 'foo' |
| 112 | + system.mkdir('foo', true) |
| 113 | + end)), |
| 114 | + test('normal operation', in_tmpdir(function () system.mkdir 'foo' end)), |
| 115 | + }, |
| 116 | + |
| 117 | + |
| 118 | + group 'rmdir' { |
| 119 | + test('remove empty directory', in_tmpdir(function () |
| 120 | + system.mkdir 'remove-me' |
| 121 | + system.rmdir 'remove-me' |
| 122 | + assert.are_same(system.ls(), {}) |
| 123 | + end)), |
| 124 | + test('fail if directory is not empty', in_tmpdir(function () |
| 125 | + system.mkdir('outer/inner', true) |
| 126 | + assert.error_matches(function () system.rmdir('outer') end, '.') |
| 127 | + end)), |
| 128 | + test('optionally delete recursively', in_tmpdir(function () |
| 129 | + system.mkdir('outer/inner', true) |
| 130 | + system.rmdir('outer', true) |
| 131 | + assert.are_same(system.ls(), {}) |
| 132 | + end)) |
| 133 | + }, |
| 134 | + |
| 135 | + group 'tmpdirname' { |
| 136 | + test('returns a string', function () |
| 137 | + assert.are_equal(type(system.tmpdirname()), 'string') |
| 138 | + end) |
| 139 | + }, |
| 140 | + |
| 141 | + group 'with_env' { |
| 142 | + test('resets environment', function () |
| 143 | + local outer_value = 'outer test value' |
| 144 | + local inner_value = 'inner test value' |
| 145 | + local inner_only = 'test #2' |
| 146 | + |
| 147 | + function check_env () |
| 148 | + assert.are_equal(os.getenv 'HSLUA_SYSTEM_TEST', inner_value) |
| 149 | + assert.are_equal( |
| 150 | + os.getenv 'HSLUA_SYSTEM_TEST_INNER_ONLY', |
| 151 | + inner_only |
| 152 | + ) |
| 153 | + assert.is_nil(os.getenv 'HSLUA_SYSTEM_TEST_OUTER_ONLY') |
| 154 | + end |
| 155 | + |
| 156 | + local test_env = { |
| 157 | + HSLUA_SYSTEM_TEST = inner_value, |
| 158 | + HSLUA_SYSTEM_TEST_INNER_ONLY = inner_only |
| 159 | + } |
| 160 | + system.setenv('HSLUA_SYSTEM_TEST_OUTER_ONLY', outer_value) |
| 161 | + system.setenv('HSLUA_SYSTEM_TEST', outer_value) |
| 162 | + system.with_env(test_env, check_env) |
| 163 | + assert.are_equal(system.getenv 'HSLUA_SYSTEM_TEST', outer_value) |
| 164 | + assert.is_nil(system.getenv 'HSLUA_SYSTEM_TEST_INNER_ONLY') |
| 165 | + assert.are_equal( |
| 166 | + system.getenv 'HSLUA_SYSTEM_TEST_OUTER_ONLY', |
| 167 | + outer_value |
| 168 | + ) |
| 169 | + end) |
| 170 | + }, |
| 171 | + |
| 172 | + group 'with_tmpdir' { |
| 173 | + test('no base directory given', function () |
| 174 | + assert.are_equal(system.with_tmpdir('foo', write_read_token), token) |
| 175 | + end), |
| 176 | + test('cwd as base directory', function () |
| 177 | + assert.are_equal(system.with_tmpdir('.', 'foo', write_read_token), token) |
| 178 | + end), |
| 179 | + }, |
| 180 | + |
| 181 | + group 'with_wd' { |
| 182 | + test('can change to test directory', function () |
| 183 | + system.with_wd('test', function () |
| 184 | + local cwd = system.getwd() |
| 185 | + assert.is_truthy(cwd:match 'test$') |
| 186 | + end) |
| 187 | + end), |
| 188 | + test('returns to old directory once done', function () |
| 189 | + local cwd = system.getwd() |
| 190 | + system.with_wd('test', function () end) |
| 191 | + assert.are_equal(system.getwd(), cwd) |
| 192 | + end), |
| 193 | + test('working directory is passed to callback', function () |
| 194 | + system.with_wd('test', function (path) |
| 195 | + assert.is_truthy(system.getwd():match (path .. '$')) |
| 196 | + end) |
| 197 | + end), |
| 198 | + } |
| 199 | +} |
0 commit comments