From a4e9a329949484d9cf270732d6f2664771479fd4 Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Tue, 26 Sep 2023 10:46:19 -0700 Subject: [PATCH 1/5] io trait --- .cargo/config.toml | 3 +++ .gitignore | 1 + blockset/src/io.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++ blockset/src/lib.rs | 14 ++----------- 4 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 blockset/src/io.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..a6a31675 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +[alias] +t = "test --release" +c = "tarpaulin --out Html --release --lib --fail-under 100" diff --git a/.gitignore b/.gitignore index 6985cf1b..c4ee3822 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +tarpaulin-report.html diff --git a/blockset/src/io.rs b/blockset/src/io.rs new file mode 100644 index 00000000..1ef0f84d --- /dev/null +++ b/blockset/src/io.rs @@ -0,0 +1,48 @@ +use std::io::{self, Read}; + +trait Io { + type File: Read; + fn open(&mut self, path: &str) -> io::Result; + fn read_to_string(&mut self, path: &str) -> io::Result { + let mut file = self.open(path)?; + let mut result = String::new(); + file.read_to_string(&mut result)?; + Ok(result) + } +} + +#[cfg(test)] +mod test { + use std::{ + collections::HashMap, + io::{self, Cursor}, + }; + + use super::Io; + + struct MockIo { + file_map: HashMap>, + } + + impl Io for MockIo { + type File = Cursor>; + + fn open(&mut self, path: &str) -> io::Result { + self.file_map + .get(path) + .map(|data| Cursor::new(data.clone())) + .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "file not found")) + } + } + + #[test] + fn test() { + let mut io = MockIo { + file_map: HashMap::default(), + }; + io.file_map + .insert("test.txt".to_string(), "Hello, world!".as_bytes().to_vec()); + let result = io.read_to_string("test.txt").unwrap(); + assert_eq!(result, "Hello, world!"); + } +} diff --git a/blockset/src/lib.rs b/blockset/src/lib.rs index 7d12d9af..068e9028 100644 --- a/blockset/src/lib.rs +++ b/blockset/src/lib.rs @@ -1,14 +1,4 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} +mod io; #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +mod tests {} From cf36f1b8594f8e925ae795d5ce3334096591f9f0 Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Tue, 26 Sep 2023 10:50:59 -0700 Subject: [PATCH 2/5] CI --- .github/workflows/rust.yml | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 31000a27..54494b5a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,13 +10,35 @@ env: CARGO_TERM_COLOR: always jobs: - build: + fmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: cargo fmt -- --check + clippy: runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: cargo clippy -- -D warnings + test: + strategy: + matrix: + # https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job#choosing-github-hosted-runners + os: [windows-latest, ubuntu-latest, macos-13] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - run: cargo t + + coverage: + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + - run: cargo install cargo-tarpaulin + - run: cargo c + - uses: actions/upload-artifact@v3 + with: + path: tarpaulin-report.html + if: always() \ No newline at end of file From c23035e096f2cf39713637cdecb161381fd38deb Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Tue, 26 Sep 2023 10:56:45 -0700 Subject: [PATCH 3/5] -out html --- .cargo/config.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index a6a31675..634b2004 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,3 @@ [alias] t = "test --release" -c = "tarpaulin --out Html --release --lib --fail-under 100" +c = "tarpaulin --out html --release --lib --fail-under 100" From 738ba4025ec302adf914a6b0624206281fe178f8 Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Tue, 26 Sep 2023 10:57:32 -0700 Subject: [PATCH 4/5] no lib tests --- blockset/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/blockset/src/lib.rs b/blockset/src/lib.rs index 068e9028..6352663e 100644 --- a/blockset/src/lib.rs +++ b/blockset/src/lib.rs @@ -1,4 +1 @@ mod io; - -#[cfg(test)] -mod tests {} From 4a2d3ab1c8dd40497635927773c7780ac103ee3e Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Tue, 26 Sep 2023 11:04:05 -0700 Subject: [PATCH 5/5] coverage: MacOS 13 --- .github/workflows/rust.yml | 2 +- blockset/src/io.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 54494b5a..3447c343 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -33,7 +33,7 @@ jobs: - run: cargo t coverage: - runs-on: ubuntu-latest + runs-on: macos-13 steps: - uses: actions/checkout@v3 - run: cargo install cargo-tarpaulin diff --git a/blockset/src/io.rs b/blockset/src/io.rs index 1ef0f84d..6ddb84ed 100644 --- a/blockset/src/io.rs +++ b/blockset/src/io.rs @@ -5,7 +5,7 @@ trait Io { fn open(&mut self, path: &str) -> io::Result; fn read_to_string(&mut self, path: &str) -> io::Result { let mut file = self.open(path)?; - let mut result = String::new(); + let mut result = String::default(); file.read_to_string(&mut result)?; Ok(result) }