Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual I/O #3

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
t = "test --release"
c = "tarpaulin --out html --release --lib --fail-under 100"
32 changes: 27 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: macos-13
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()
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
tarpaulin-report.html
48 changes: 48 additions & 0 deletions blockset/src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::io::{self, Read};

trait Io {
type File: Read;
fn open(&mut self, path: &str) -> io::Result<Self::File>;
fn read_to_string(&mut self, path: &str) -> io::Result<String> {
let mut file = self.open(path)?;
let mut result = String::default();
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<String, Vec<u8>>,
}

impl Io for MockIo {
type File = Cursor<Vec<u8>>;

fn open(&mut self, path: &str) -> io::Result<Self::File> {
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!");
}
}
15 changes: 1 addition & 14 deletions blockset/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
mod io;