Skip to content

Commit

Permalink
Accept io.Data as input. (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Kasper Lund <[email protected]>
  • Loading branch information
floitsch and kasperl authored Jun 6, 2024
1 parent 9053814 commit d0f3890
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ jobs:
with:
toit-version: ${{ matrix.toit-version }}

- name: Call ar
run: |
# On macOS, the test would take 30+ seconds if we don't call ar first.
# Not entirely clear why...
ar --version || true
- name: Test
run: |
make test
6 changes: 3 additions & 3 deletions src/ar.toit
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class ArWriter:
by 'ar' when using the 'D' (deterministic) option. For example, the
modification date is set to 0 (epoch time).
*/
add name/string content -> none:
add name/string content/io.Data -> none:
if name.size > FILE-NAME-SIZE_: throw "Filename too long"
write-ar-file-header_ name content.size
write-ar-file-header_ name content.byte-size
writer_.write content
if needs-padding_ content.size:
if needs-padding_ content.byte-size:
writer_.write PADDING-STRING_

/**
Expand Down
19 changes: 13 additions & 6 deletions tests/ar_test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import system
TESTS ::= [
{:},
{"odd": "odd".to-byte-array},
{"even": "even".to-byte-array},
{"even": "even"},
{
"even": "even".to-byte-array,
"odd": "odd".to-byte-array,
"odd": "odd",
},
{
"odd": "odd".to-byte-array,
"even": "even".to-byte-array,
"even": "even",
},
{
"binary": #[0, 1, 2, 255],
"newlines": "\n\n\n\n".to-byte-array,
"newlines2": "\a\a\a\a\a".to-byte-array,
"newlines2": "\a\a\a\a\a",
"big": ("the quick brown fox jumps over the lazy dog" * 1000).to-byte-array
},
]
Expand All @@ -35,6 +35,8 @@ write-ar file-mapping/Map --add-with-ar-file/bool=false:
ar-writer := ArWriter writer
file-mapping.do: |name content|
if add-with-ar-file:
// The `ArFile` only takes byte arrays.
if content is string: content = content.to-byte-array
ar-writer.add
ArFile name content
else:
Expand All @@ -51,6 +53,7 @@ run-test file-mapping/Map tmp-dir [generate-ar]:
count++
seen.add file.name
expected := file-mapping[file.name]
if expected is string: expected = expected.to-byte-array
expect-equals expected file.content
// No file was seen twice.
expect-equals seen.size count
Expand All @@ -63,6 +66,7 @@ run-test file-mapping/Map tmp-dir [generate-ar]:
count++
seen.add file-offsets.name
expected := file-mapping[file-offsets.name]
if expected is string: expected = expected.to-byte-array
actual := ba.copy file-offsets.from file-offsets.to
expect-equals expected actual
// No file was seen twice.
Expand All @@ -75,15 +79,17 @@ run-test file-mapping/Map tmp-dir [generate-ar]:
file-mapping.do: |name content|
last-name = name
file := ar-reader.find name
expect-equals content file.content
expected := content is ByteArray ? content : content.to-byte-array
expect-equals expected file.content

ar-reader = ArReader.from-bytes ba
// We should find all files if we advance from top to bottom.
file-mapping.do: |name content|
last-name = name
file-offsets := ar-reader.find --offsets name
actual := ba.copy file-offsets.from file-offsets.to
expect-equals content actual
expected := content is ByteArray ? content : content.to-byte-array
expect-equals expected actual

ar-reader = ArReader (io.Reader ba)
ar-file := ar-reader.find "not there"
Expand Down Expand Up @@ -114,6 +120,7 @@ run-test file-mapping/Map tmp-dir [generate-ar]:
stream.close
file-mapping.do: |name expected-content|
actual := extract test-path name
if expected-content is string: expected-content = expected-content.to-byte-array
expect-equals expected-content actual

extract archive-file contained-file -> ByteArray:
Expand Down

0 comments on commit d0f3890

Please sign in to comment.