diff --git a/.woodpecker.yml b/.woodpecker.yml index 2c9bb67..ec38d74 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -1,10 +1,96 @@ -pipeline: - build: - image: rust +variables: + - &rust_image "docker.io/rust:1.76" + - &slow_check_paths + - path: + # rust source code + - "crates/**" + - "src/**" + - "tests/**" + - "**/Cargo.toml" + - "Cargo.lock" + # database migrations + - "migrations/**" + # config files and scripts used by ci + - ".woodpecker.yml" + +steps: + + toml_fmt: + image: docker.io/tamasfe/taplo:0.8.1 + commands: + - taplo format --check + + + cargo_fmt: + image: docker.io/rustlang/rust:nightly + environment: + # store cargo data in repo folder so that it gets cached between steps + CARGO_HOME: .cargo_home commands: - rustup component add rustfmt - - cargo --version - cargo fmt --check + + cargo_machete: + image: *rust_image + commands: + - wget https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz + - tar -xvf cargo-binstall-x86_64-unknown-linux-musl.tgz + - cp cargo-binstall /usr/local/cargo/bin + - cargo binstall -y cargo-machete + - cargo machete + + ignored_files: + image: docker.io/alpine:latest + commands: + - apk add git + - IGNORED=$(git ls-files --cached -i --exclude-standard) + - if [[ "$IGNORED" ]]; then echo "Ignored files present:\n$IGNORED\n"; exit 1; fi + + check: + image: *rust_image + environment: + CARGO_HOME: .cargo_home + commands: + - cargo check + when: *slow_check_paths + + cargo_clippy: + image: *rust_image + environment: + CARGO_HOME: .cargo_home + commands: + - rustup component add clippy + - cargo clippy --tests --all-targets -- -D warnings + when: *slow_check_paths + + cargo_build: + image: *rust_image + environment: + CARGO_HOME: .cargo_home + commands: - cargo build - - cargo test + - mv target/debug/skip target/skip + when: *slow_check_paths + + cargo_test: + image: *rust_image + environment: + RUST_BACKTRACE: "1" + CARGO_HOME: .cargo_home + commands: + - cargo test --no-fail-fast + when: *slow_check_paths + + integration_test: + image: *rust_image + commands: - ./test.sh + + publish_to_crates_io: + image: *rust_image + commands: + - cargo login "$CARGO_REGISTRY_TOKEN" + - cargo publish --registry crates-io --no-verify --allow-branch "${CI_COMMIT_TAG}" --yes + secrets: [cargo_api_token] + when: + event: tag diff --git a/src/lib.rs b/src/lib.rs index 397c4b0..a61003a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> { None => Box::new(BufReader::new(std::io::stdin())), }; if let Some(line) = &cli.line { - skip_lines_matching(&cli, reader, writer, line) + skip_lines_matching(cli, reader, writer, line) } else if let Some(ref token) = cli.token { skip_tokens(cli, reader, writer, token) } else { @@ -49,13 +49,9 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> { // skip a number of lines fn skip_lines(cli: &Cli, reader: Box, writer: &mut impl Write) -> Result<()> { - let mut counter = 0usize; - for current_line in reader.lines() { - if let Ok(current_line) = current_line { - if counter >= cli.lines { - writeln!(writer, "{}", current_line)?; - } - counter += 1; + for (counter, current_line) in reader.lines().map_while(Option::Some).flatten().enumerate() { + if counter >= cli.lines { + writeln!(writer, "{}", current_line)?; } } Ok(()) @@ -69,14 +65,12 @@ fn skip_lines_matching( line: &str, ) -> Result<()> { let mut counter = 0usize; - for current_line in reader.lines() { - if let Ok(current_line) = current_line { - if counter >= cli.lines { - writeln!(writer, "{}", current_line)?; - } - if line == current_line { - counter += 1; - } + for current_line in reader.lines().map_while(Option::Some).flatten() { + if counter >= cli.lines { + writeln!(writer, "{}", current_line)?; + } + if line == current_line { + counter += 1; } } Ok(()) @@ -92,18 +86,16 @@ fn skip_tokens( ) -> Result<()> { let mut counter = 0usize; - for current_line in reader.lines() { - if let Ok(current_line) = current_line { - if counter >= cli.lines { - writeln!(writer, "{}", current_line)?; - } - if current_line.contains(&token) { - if cli.ignore_extras { - counter += 1; - } else { - let occurances = current_line.matches(&token).count(); - counter += occurances; - } + for current_line in reader.lines().map_while(Option::Some).flatten() { + if counter >= cli.lines { + writeln!(writer, "{}", current_line)?; + } + if current_line.contains(token) { + if cli.ignore_extras { + counter += 1; + } else { + let occurances = current_line.matches(&token).count(); + counter += occurances; } } } @@ -151,10 +143,7 @@ mod tests { skip(&cli, &mut lines)?; //then - assert_eq!( - String::from_utf8(lines)?, - vec!["alpha", "gamma\n"].join("\n") - ); + assert_eq!(String::from_utf8(lines)?, ["alpha", "gamma\n"].join("\n")); Ok(()) } @@ -196,7 +185,7 @@ mod tests { //then assert_eq!( String::from_utf8(lines)?, - vec![ + [ "Or help one fainting robin", "Unto his nest again,", "I shall not live in vain.\n" @@ -224,7 +213,7 @@ mod tests { //then assert_eq!( String::from_utf8(lines)?, - vec![ + [ //Lorem ipsum dolor sit amet, -- +2 = 2 //consectetur adipiscing elit, //sed do eiusmod tempor incididunt -- +1 = 3 @@ -257,7 +246,7 @@ mod tests { //then assert_eq!( String::from_utf8(lines)?, - vec![ + [ //Lorem ipsum dolor sit amet, -- 1 //consectetur adipiscing elit, //sed do eiusmod tempor incididunt -- 2 diff --git a/test.sh b/test.sh index e4c3aa8..b78f511 100755 --- a/test.sh +++ b/test.sh @@ -2,11 +2,14 @@ set -e -SKIP="./target/debug/skip" +echo "PWD: $PWD" +ls -l +ls -l target +SKIP="./target/skip" DIFF="diff -u --color" if test ! -x $SKIP ; then - echo "File missing: $SKIP - try 'zig build'" + echo "File missing: $SKIP - try 'cargo build'" exit 1 fi