Merge pull request #1 from alexwlchan/add-ci
- ID
3c7a3c4- date
2021-11-27 08:28:58+00:00- author
Alex Chan <alex@alexwlchan.net>- parents
46aee3b,20431ef- message
Merge pull request #1 from alexwlchan/add-ci Run some tests with GitHub Actions- changed files
Changed files
.github/workflows/build.yml (0) → .github/workflows/build.yml (374)
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..8680083
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,18 @@
+on: [push]
+name: build
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - uses: actions-rs/toolchain@v1
+ with:
+ toolchain: stable
+ - name: build
+ uses: actions-rs/cargo@v1
+ with:
+ command: build
+ - name: test
+ uses: actions-rs/cargo@v1
+ with:
+ command: test
Cargo.lock (9947) → Cargo.lock (13035)
diff --git a/Cargo.lock b/Cargo.lock
index 72ec702..efc6aee 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -27,6 +27,20 @@ dependencies = [
]
[[package]]
+name = "assert_cmd"
+version = "2.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e996dc7940838b7ef1096b882e29ec30a3149a3a443cdc8dba19ed382eca1fe2"
+dependencies = [
+ "bstr",
+ "doc-comment",
+ "predicates",
+ "predicates-core",
+ "predicates-tree",
+ "wait-timeout",
+]
+
+[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -50,6 +64,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
+name = "bstr"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
+dependencies = [
+ "lazy_static",
+ "memchr",
+ "regex-automata",
+]
+
+[[package]]
name = "bytemuck"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -108,9 +133,22 @@ dependencies = [
]
[[package]]
+name = "difflib"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
+
+[[package]]
+name = "doc-comment"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
+
+[[package]]
name = "dominant_colours"
version = "1.0.0"
dependencies = [
+ "assert_cmd",
"clap",
"image",
"kmeans_colors",
@@ -118,6 +156,12 @@ dependencies = [
]
[[package]]
+name = "either"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
+
+[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -154,6 +198,15 @@ dependencies = [
]
[[package]]
+name = "itertools"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
+dependencies = [
+ "either",
+]
+
+[[package]]
name = "jpeg-decoder"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -171,12 +224,24 @@ dependencies = [
]
[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
name = "libc"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
[[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
name = "miniz_oxide"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -267,6 +332,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
[[package]]
+name = "predicates"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95e5a7689e456ab905c22c2b48225bb921aba7c8dfa58440d68ba13f6222a715"
+dependencies = [
+ "difflib",
+ "itertools",
+ "predicates-core",
+]
+
+[[package]]
+name = "predicates-core"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451"
+
+[[package]]
+name = "predicates-tree"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "338c7be2905b732ae3984a2f40032b5e94fd8f52505b186c7d4d68d193445df7"
+dependencies = [
+ "predicates-core",
+ "termtree",
+]
+
+[[package]]
name = "proc-macro2"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -315,6 +407,12 @@ dependencies = [
]
[[package]]
+name = "regex-automata"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
+
+[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -332,6 +430,12 @@ dependencies = [
]
[[package]]
+name = "termtree"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13a4ec180a2de59b57434704ccfad967f789b12737738798fa08798cd5824c16"
+
+[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -359,6 +463,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
+name = "wait-timeout"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
+dependencies = [
+ "libc",
+]
+
+[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
Cargo.toml (378) → Cargo.toml (399)
diff --git a/Cargo.toml b/Cargo.toml
index 7c8791b..d35a835 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,7 @@ version = "1.0.0"
edition = "2018"
[dependencies]
+assert_cmd = "2.0.2"
clap = "2.33"
[dependencies.kmeans_colors]
src/main.rs (3550) → src/main.rs (6582)
diff --git a/src/main.rs b/src/main.rs
index 395e4fe..857a930 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -93,3 +93,106 @@ fn main() {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use std::str;
+
+ use assert_cmd::assert::OutputAssertExt;
+ use assert_cmd::Command;
+
+ // Note: for the purposes of these tests, I mostly trust the k-means code
+ // provided by the external library.
+
+ #[test]
+ fn it_prints_the_color_with_ansi_escape_codes() {
+ let output = get_success(&["./src/tests/red.png", "--count=1"]);
+
+ assert_eq!(output.exit_code, 0);
+
+ assert!(
+ output.stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n" ||
+ output.stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n",
+ "stdout = {:?}", output.stdout
+ );
+
+ assert_eq!(output.stderr, "");
+ }
+
+ #[test]
+ fn it_omits_the_escape_codes_with_no_palette() {
+ let output = get_success(&["./src/tests/red.png", "--count=1"]);
+
+ assert_eq!(output.exit_code, 0);
+
+ assert!(
+ output.stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n" ||
+ output.stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n",
+ "stdout = {:?}", output.stdout
+ );
+
+ assert_eq!(output.stderr, "");
+ }
+
+ #[test]
+ fn it_defaults_to_five_colours() {
+ let output = get_success(&["./src/tests/noise.jpg"]);
+
+ assert_eq!(output.stdout.matches("\n").count(), 5, "stdout = {:?}", output.stdout);
+ }
+
+ #[test]
+ fn it_lets_you_choose_the_count() {
+ let output = get_success(&["./src/tests/noise.jpg", "--count=8"]);
+
+ assert_eq!(output.stdout.matches("\n").count(), 8, "stdout = {:?}", output.stdout);
+ }
+
+ #[test]
+ fn it_fails_if_you_pass_an_invalid_count() {
+ let output = get_failure(&["./src/tests/red.png", "--count=NaN"]);
+
+ assert_eq!(output.exit_code, 1);
+ assert_eq!(output.stdout, "");
+ assert_eq!(output.stderr, "error: Invalid value: The argument 'NaN' isn't a valid value\n");
+ }
+
+ struct DcOutput {
+ exit_code: i32,
+ stdout: String,
+ stderr: String,
+ }
+
+ fn get_success(args: &[&str]) -> DcOutput {
+ let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
+ let output = cmd
+ .args(args)
+ .unwrap()
+ .assert()
+ .success()
+ .get_output()
+ .to_owned();
+
+ DcOutput {
+ exit_code: output.status.code().unwrap(),
+ stdout: str::from_utf8(&output.stdout).unwrap().to_owned(),
+ stderr: str::from_utf8(&output.stderr).unwrap().to_owned(),
+ }
+ }
+
+ fn get_failure(args: &[&str]) -> DcOutput {
+ let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
+ let output = cmd
+ .args(args)
+ .unwrap_err()
+ .as_output()
+ .unwrap()
+ .to_owned();
+
+ DcOutput {
+ exit_code: output.status.code().unwrap(),
+ stdout: str::from_utf8(&output.stdout).unwrap().to_owned(),
+ stderr: str::from_utf8(&output.stderr).unwrap().to_owned(),
+ }
+ }
+}
src/tests/noise.jpg (0) → src/tests/noise.jpg (25175)
diff --git a/src/tests/noise.jpg b/src/tests/noise.jpg
new file mode 100644
index 0000000..0998e82
Binary files /dev/null and b/src/tests/noise.jpg differ
src/tests/red.png (0) → src/tests/red.png (725)
diff --git a/src/tests/red.png b/src/tests/red.png
new file mode 100644
index 0000000..80aea6e
Binary files /dev/null and b/src/tests/red.png differ