File: test-zip.lua

package info (click to toggle)
haskell-hslua-module-zip 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100 kB
  • sloc: haskell: 304; makefile: 3
file content (210 lines) | stat: -rw-r--r-- 5,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
--
-- Tests for the system module
--
local zip = require 'zip'
local tasty = require 'tasty'
local system = require 'system'

local group = tasty.test_group
local test = tasty.test_case
local assert = tasty.assert

local make_sample_archive = function (opts)
  opts = opts or {}
  local filename = opts.filename or 'greetings.txt'
  local contents = opts.contents or 'Hello Bob!\n'
  return system.with_tmpdir('archive', function (tmpdir)
    return system.with_wd(tmpdir, function ()
      local fh = io.open(filename, 'w')
      fh:write(contents)
      fh:close()
      return zip{filename}
    end)
  end)
end

local empty_archive = '\80\75\5\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
-- Check existence static fields
return {
  group 'Archive' {
    test('empty archive', function ()
      assert.are_equal(type(zip.Archive()), 'userdata')
    end),
  },

  group 'zip' {
    test('archive with file', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local fh = io.open(filename, 'w')
          fh:write('Hi Mom!\n')
          fh:close()
          assert.are_equal(
            type(zip.zip{filename}),
            'userdata'
          )
        end)
      end)
    end),

    test('recursive', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local dirname = 'greetings'
          local filename = dirname .. '/' .. 'french.txt'
          system.mkdir(dirname)
          local fh = io.open(filename, 'w')
          fh:write('Bonjour!\n')
          fh:close()
          local archive = zip.zip({dirname}, {recursive=true})
          assert.are_equal(
            archive.entries[2].path,
            filename
          )
        end)
      end)
    end),

    test('module metamethod', function()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local fh = io.open(filename, 'w')
          fh:write('Hi Mom!\n')
          fh:close()
          assert.are_equal(
            zip.zip{filename}:bytestring(),
            zip{filename}:bytestring()
          )
        end)
      end)
    end)
  },

  group 'extract' {
    test('archive with file', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local archive = make_sample_archive{
            filename = filename,
            contents = 'Hi Mom!\n',
          }
          archive:extract()

          assert.are_equal(system.ls()[1], filename)
          assert.are_equal(
            io.open(filename):read 'a',
            'Hi Mom!\n'
          )
        end)
      end)
    end),
    test('to destination directory', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local archive = make_sample_archive{
            filename = filename,
            contents = 'Hi Mom!\n',
          }
          archive:extract{destination = 'foo'}
          assert.are_equal(system.ls()[1], 'foo')
          assert.are_equal(
            io.open('foo/' .. filename):read 'a',
            'Hi Mom!\n'
          )
        end)
      end)
    end),
  },

  group 'entries' {
    test('empty archive', function ()
      assert.are_equal(#zip.Archive().entries, 0)
    end),

    test('archive with file', function ()
      local archive = make_sample_archive{filename='greetings.txt'}
      assert.are_equal(
        archive.entries[1].path,
        'greetings.txt'
      )
    end),

    test('has type "ZipEntry list"', function ()
      local archive = make_sample_archive()
      assert.are_equal(getmetatable(archive.entries).__name, 'ZipEntry list')
    end),

    test('has `insert` method', function ()
      local archive = make_sample_archive()
      assert.are_equal(type(archive.entries.insert), 'function')
    end),
  },

  group 'read_entry' {
    test('has correct file path', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local fh = io.open(filename, 'wb')
          fh:write('Hi Mom!\n')
          fh:close()
          local entry = zip.read_entry(filename)
          assert.are_equal(entry.path, filename)
        end)
      end)
    end),

    test('has contents', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local fh = io.open(filename, 'wb')
          fh:write('Hallo!\n')
          fh:close()
          local entry = zip.read_entry(filename)
          assert.are_equal(
            entry:contents('foo'),
            'Hallo!\n'
          )
        end)
      end)
    end),

    -- Can't reliably test this for actual symlinks, as Windows doesn't
    -- support them. Only the behavior for normal files is tested.
    test('has symlink function', function ()
      system.with_tmpdir('archive', function (tmpdir)
        system.with_wd(tmpdir, function ()
          local filename = 'greetings.txt'
          local fh = io.open(filename, 'w')
          fh:write('Hallo!\n')
          fh:close()
          local entry = zip.read_entry(filename)
          assert.is_nil(entry:symlink())
        end)
      end)
    end)
  },

  group 'bytestring' {
    test('empty archive', function ()
      assert.are_equal(zip.Archive():bytestring(), empty_archive)
    end),
  },

  group 'Archive constructor' {
    test('empty archive', function ()
      assert.are_equal(type(zip.Archive(empty_archive)), 'userdata')
    end),
    test('misformed archive', function ()
      assert.error_matches(
        function () zip.Archive(empty_archive:sub(2)) end,
        ''
      )
    end),
  },
}