Skip to content

Commit

Permalink
tests: port from unmaintained crate encoding to encoding_rs
Browse files Browse the repository at this point in the history
Closes: #106
  • Loading branch information
cgzones authored and otavio committed Mar 20, 2023
1 parent 5ad0368 commit 5e7bbdd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tempfile = "3.1"
argh = "0.1"
async-std = { version = "1.6.3", features = ["attributes"] }
tokio = { version = "1.0.0", features = ["fs", "net"] }
encoding = "0.2"
encoding_rs = "0.8.32"

[target.'cfg(target_env = "msvc")'.build-dependencies]
vcpkg = "0.2"
Expand Down
23 changes: 18 additions & 5 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,23 @@ fn uncompress_to_dir_with_utf8_pathname() {

#[test]
fn uncompress_to_dir_with_cjk_pathname() {
use encoding::{all::*, DecoderTrap, Encoding};
use encoding_rs::{GBK, SHIFT_JIS};

let dir = tempfile::TempDir::new().expect("Failed to create the tmp directory");
let mut source_utf8 = std::fs::File::open("tests/fixtures/encoding-utf8.zip").unwrap();
let mut source_gbk = std::fs::File::open("tests/fixtures/encoding-gbk.zip").unwrap();
let mut source_sjis = std::fs::File::open("tests/fixtures/encoding-sjis.zip").unwrap();
let decode_gbk = |bytes: &[u8]| Ok(GBK.decode(bytes, DecoderTrap::Strict)?);
let decode_sjis = |bytes: &[u8]| Ok(WINDOWS_31J.decode(bytes, DecoderTrap::Strict)?);
let decode_gbk = |bytes: &[u8]| {
GBK.decode_without_bom_handling_and_without_replacement(bytes)
.map(String::from)
.ok_or(Error::Encoding(std::borrow::Cow::Borrowed("GBK failure")))
};
let decode_sjis = |bytes: &[u8]| {
SHIFT_JIS
.decode_without_bom_handling_and_without_replacement(bytes)
.map(String::from)
.ok_or(Error::Encoding(std::borrow::Cow::Borrowed("GBK failure")))
};

uncompress_archive_with_encoding(&mut source_utf8, dir.path(), Ownership::Ignore, decode_utf8)
.expect("Failed to uncompress the file");
Expand Down Expand Up @@ -600,11 +609,15 @@ fn iterate_7z() {

#[test]
fn iterate_zip_with_cjk_pathname() {
use encoding::{all::*, DecoderTrap, Encoding};
use encoding_rs::GBK;

let source = std::fs::File::open("tests/fixtures/encoding-gbk-tree.zip").unwrap();

let decode_gbk = |bytes: &[u8]| Ok(GBK.decode(bytes, DecoderTrap::Strict)?);
let decode_gbk = |bytes: &[u8]| {
GBK.decode_without_bom_handling_and_without_replacement(bytes)
.map(String::from)
.ok_or(Error::Encoding(std::borrow::Cow::Borrowed("GBK failure")))
};
let contents = collect_iterate_results_with_encoding(source, decode_gbk);

let expected: Vec<(String, usize)> = vec![
Expand Down

0 comments on commit 5e7bbdd

Please sign in to comment.