From 78079ef218f2758feb796e05c050cf1fffd954ac Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Fri, 1 Mar 2019 16:18:13 +0200 Subject: [PATCH 01/11] Add decode tests using quirc and SVG to PNG image encoding --- .travis.yml | 6 +++++- Cargo.toml | 9 +++++++++ src/qrcode.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 596e413..937cac3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,12 @@ rust: - stable - nightly cache: cargo +before_install: + - sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa + - sudo apt-get update -qq + - sudo apt-get install qt5-default qttools5-dev-tools before_script: - rustup component add clippy script: - cargo clippy --all-targets --all-features -- -D warnings - - cargo test --release + - QT_DIR=/usr/local/opt/qt cargo test --release diff --git a/Cargo.toml b/Cargo.toml index 77f787c..fd2ff7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,12 @@ hex = "0.3.1" strum = "0.13.0" strum_macros = "0.13.0" svgdom = "0.16.0" + +[dev-dependencies] +quirc = "0.0.1" +tempfile = "3.0.7" +image = "0.19.0" + +[dev-dependencies.resvg] +version = "0.5.0" +features = ["qt-backend"] diff --git a/src/qrcode.rs b/src/qrcode.rs index c0454d0..fc24d85 100644 --- a/src/qrcode.rs +++ b/src/qrcode.rs @@ -143,7 +143,25 @@ impl<'a> QRCode<'a> { #[cfg(test)] mod tests { + extern crate image; + extern crate quirc; + extern crate tempfile; + use super::*; + use quirc::{EccLevel, QrCoder}; + use resvg::usvg; + use tempfile::Builder; + + impl From for EccLevel { + fn from(ec: ECLevel) -> Self { + match ec { + ECLevel::L => EccLevel::L, + ECLevel::M => EccLevel::M, + ECLevel::Q => EccLevel::Q, + ECLevel::H => EccLevel::H, + } + } + } #[test] fn test_default_options() { @@ -157,9 +175,32 @@ mod tests { #[test] fn $name() { let (options, data, expected) = $value; + + // Encode the data to an SVG and check its contents. let qr = QRCode::new(&options); let encoded = qr.encode_svg(data.as_bytes()); assert_eq!(encoded, expected); + + // Convert the SVG to a PNG. + let temp_file = Builder::new() + .suffix(".png") + .tempfile().unwrap(); + let opt = resvg::Options::default(); + let rtree = usvg::Tree::from_data(encoded.as_bytes(), &opt.usvg).unwrap(); + let temp_image = resvg::backend_qt::render_to_image(&rtree, &opt).unwrap(); + let temp_path = temp_file.path().to_str().unwrap(); + temp_image.save(temp_path); + println!("Path is {}", temp_path); + + // Use quirc to decode and verify that the data was properly encoded. + let image = image::open(temp_path).unwrap().to_luma(); + let mut quirc = QrCoder::new().unwrap(); + + let mut codes = quirc.codes(&image, image.width(), image.height()).unwrap(); + let code = codes.next().unwrap().unwrap(); + assert_eq!(code.payload, data.as_bytes()); + assert_eq!(code.ecc_level, EccLevel::from(options.ec_level)); + assert!(codes.next().is_none()); } )* } From c8271b9acf4febafb0879e0147ea94b11bb62251 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Fri, 1 Mar 2019 17:46:31 +0200 Subject: [PATCH 02/11] Run tests with `QT_DIR` --- release.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release.sh b/release.sh index d3a1fe3..714540d 100755 --- a/release.sh +++ b/release.sh @@ -8,7 +8,7 @@ echo "Running clippy..." cargo clippy --all-targets --all-features -- -D warnings echo "Running tests..." -cargo test --release +QT_DIR=/usr/local/opt/qt cargo test --release echo "Building v${VERSION} for Mac OS..." APPLE_RELEASE="target/qrcode-cli-${VERSION}-osx.tgz" From 33fa524f2360c0bb8d0ed97c1fee31e09353b3e6 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Fri, 1 Mar 2019 17:46:45 +0200 Subject: [PATCH 03/11] Remove trace --- src/qrcode.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/qrcode.rs b/src/qrcode.rs index fc24d85..58fff69 100644 --- a/src/qrcode.rs +++ b/src/qrcode.rs @@ -190,7 +190,6 @@ mod tests { let temp_image = resvg::backend_qt::render_to_image(&rtree, &opt).unwrap(); let temp_path = temp_file.path().to_str().unwrap(); temp_image.save(temp_path); - println!("Path is {}", temp_path); // Use quirc to decode and verify that the data was properly encoded. let image = image::open(temp_path).unwrap().to_luma(); From ab37059bd01a88ccf8a6da0060b4f59a320a6f85 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:12:26 +0200 Subject: [PATCH 04/11] At the moment, resvg is unable to render SVG with test, so we'd have to skip extended tests for this case. --- src/qrcode.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/qrcode.rs b/src/qrcode.rs index 58fff69..e908268 100644 --- a/src/qrcode.rs +++ b/src/qrcode.rs @@ -181,6 +181,12 @@ mod tests { let encoded = qr.encode_svg(data.as_bytes()); assert_eq!(encoded, expected); + // TODO: At the moment, resvg is unable to render SVG with test, so we'd have to skip extended tests for + // this case. + if (options.embed) { + return; + } + // Convert the SVG to a PNG. let temp_file = Builder::new() .suffix(".png") From a51d0731ea928283051a4ee7b5fb331da9e0edd0 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:12:35 +0200 Subject: [PATCH 05/11] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fd2ff7b..db74788 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "qrcode-cli" -version = "0.3.1" +version = "0.3.2" authors = ["Leonid Beder "] edition = "2018" From 78d9f264f2ac73d7d4a2cf065191ae495a005991 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:17:40 +0200 Subject: [PATCH 06/11] Remove unnecessary parentheses --- src/qrcode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qrcode.rs b/src/qrcode.rs index e908268..3dbdec7 100644 --- a/src/qrcode.rs +++ b/src/qrcode.rs @@ -183,7 +183,7 @@ mod tests { // TODO: At the moment, resvg is unable to render SVG with test, so we'd have to skip extended tests for // this case. - if (options.embed) { + if options.embed { return; } From f05b724017a980a9a2748d799fe90bbb7fbce8b5 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:22:56 +0200 Subject: [PATCH 07/11] Temporarily fetch nightly clippy from nightly-2019-02-08 --- .travis.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 937cac3..1238592 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,13 @@ before_install: - sudo apt-get update -qq - sudo apt-get install qt5-default qttools5-dev-tools before_script: - - rustup component add clippy + - | + if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then + rustup toolchain add nightly-2019-02-08 + rustup component add --toolchain nightly-2019-02-08 clippy + else + rustup component add clippy + fi script: - cargo clippy --all-targets --all-features -- -D warnings - QT_DIR=/usr/local/opt/qt cargo test --release From 92606fb5e5e9bb55719cae8398a8a0cfd9ed5502 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:25:27 +0200 Subject: [PATCH 08/11] Fix comment --- src/qrcode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qrcode.rs b/src/qrcode.rs index 3dbdec7..34a0c31 100644 --- a/src/qrcode.rs +++ b/src/qrcode.rs @@ -181,7 +181,7 @@ mod tests { let encoded = qr.encode_svg(data.as_bytes()); assert_eq!(encoded, expected); - // TODO: At the moment, resvg is unable to render SVG with test, so we'd have to skip extended tests for + // TODO: At the moment, resvg is unable to render SVG with text, so we'd have to skip extended tests for // this case. if options.embed { return; From 5e616842d86fc92271a77e518566f60adf198910 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:25:37 +0200 Subject: [PATCH 09/11] Add changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..80a26e2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 0.3.2 (2019-03-02) + +- Add decoding tests (but only for tests without text embedding, since resvg is unable to render SVG with text). From 4f40c9481da3b683239ce79347692add1e2ff1bc Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 10:28:50 +0200 Subject: [PATCH 10/11] Pin nightly to nightly-2019-02-08 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1238592..ea93161 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: rust rust: - stable - - nightly + - nightly-2019-02-08 cache: cargo before_install: - sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa From 148cb27d4d0fbe54f435820e2c61c27467e65b47 Mon Sep 17 00:00:00 2001 From: Leonid Beder Date: Sat, 2 Mar 2019 11:06:30 +0200 Subject: [PATCH 11/11] Update README.md --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 2534f11..5f2f499 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,30 @@ Now you can build it: CROSS_COMPILE=x86_64-linux-musl- cargo build --target=x86_64-unknown-linux-musl ``` +## Test + +To run the tests, you'd need to have the `qt` backend installed: + +For Mac OS: + +```bash +brew install qt +``` + +For Linux (Ubuntu): + +```bash +sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa +sudo apt-get update -qq +sudo apt-get install qt5-default qttools5-dev-tools +``` + +You would need to pass `qt` root directory via the `QT_DIR` environment variable. For example: + +```bash +QT_DIR=/usr/local/opt/qt cargo test +``` + ## Example Let's see few QR code generation examples: