From b0a60a1173d706ae1f6437a2bab3050da8479777 Mon Sep 17 00:00:00 2001 From: Bryan Conn <30739012+bconn98@users.noreply.github.com> Date: Wed, 6 Dec 2023 18:08:11 -0500 Subject: [PATCH] ci: Update CI and add Code Coverage (#326) --- .editorconfig | 11 ++ .github/workflows/audit.yml | 23 ++++ .github/workflows/coverage.yml | 35 ++++++ .github/workflows/editor-check.yml | 20 +++ .github/workflows/lint.yml | 45 +++++++ .github/workflows/main.yml | 188 +++++++++++------------------ README.md | 38 +++--- docs/Configuration.md | 74 ++++++------ examples/sample_config.yml | 20 +-- src/append/rolling_file/mod.rs | 44 +++---- src/config/raw.rs | 36 +++--- src/encode/json.rs | 4 +- src/encode/pattern/mod.rs | 10 +- test/log.yml | 4 +- 14 files changed, 318 insertions(+), 234 deletions(-) create mode 100644 .github/workflows/audit.yml create mode 100644 .github/workflows/coverage.yml create mode 100644 .github/workflows/editor-check.yml create mode 100644 .github/workflows/lint.yml diff --git a/.editorconfig b/.editorconfig index c8e4a9c9..51b58b71 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,5 +14,16 @@ insert_final_newline = true indent_style = space indent_size = 4 +[*.rs] +indent_size = false + [*.md] trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 + +# Ignore License files +[LICENSE-*] +indent_size = unset +indent_style = unset diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml new file mode 100644 index 00000000..f8aefd2a --- /dev/null +++ b/.github/workflows/audit.yml @@ -0,0 +1,23 @@ +--- +name: Security audit +on: + push: + paths: + - '**/Cargo.toml' + - '**/Cargo.lock' + - '.github/workflows/audit.yml' +jobs: + cargo-audit: + name: Audit the baseline for CVEs + runs-on: ubuntu-latest + steps: + - name: Checkout the source code + uses: actions/checkout@v4 + + - uses: cargo-bins/cargo-binstall@main + + - name: Install cargo-audit + run: cargo binstall -y cargo-audit + + - name: Audit + run: cargo audit diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 00000000..2ca0f327 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,35 @@ +--- +name: coverage + +on: + push: + branches: + - main + - devel + pull_request: + branches: + - main + - devel + +jobs: + test: + # https://github.com/xd009642/tarpaulin#github-actions + name: coverage + runs-on: ubuntu-latest + container: + image: xd009642/tarpaulin:develop-nightly + options: --security-opt seccomp=unconfined + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Generate code coverage + run: | + cargo +nightly tarpaulin --verbose --all-features --workspace --timeout 120 --out xml + env: + USER: "test-user" + + - name: Upload to codecov.io + uses: codecov/codecov-action@v3 + with: + fail_ci_if_error: true diff --git a/.github/workflows/editor-check.yml b/.github/workflows/editor-check.yml new file mode 100644 index 00000000..3d20538a --- /dev/null +++ b/.github/workflows/editor-check.yml @@ -0,0 +1,20 @@ +--- +name: EditorConfig Checker + +on: + push: + branches: + - main + - devel + pull_request: + branches: + - main + - devel + +jobs: + editorconfig: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: editorconfig-checker/action-editorconfig-checker@main + - run: editorconfig-checker diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..7571ef9d --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,45 @@ +--- +name: Lint Jobs + +on: + push: + branches: + - main + - devel + pull_request: + branches: + - main + - devel + +jobs: + rust-lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout the source code + uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt, clippy + + - name: Run cargo check + run: cargo check + + - name: Run rustfmt + run: cargo fmt -- --check + + - name: Run Clippy + run: cargo clippy --all-features + env: + RUSTFLAGS: -D warnings + + docs-lint: + name: Docs Lint + runs-on: ubuntu-latest + steps: + - name: Markdown Linting Action + uses: avto-dev/markdown-lint@v1 + with: + config: .markdownlint.yml + args: docs/Configuration.md README.md diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a812a4c1..bbdd43d5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,123 +1,73 @@ -name: CI +--- +name: Build CI on: - push: - branches: - - main - - devel - pull_request: - branches: - - main - - devel + push: + branches: + - main + - devel + pull_request: + branches: + - main + - devel jobs: - rust-lint: - name: Lint - runs-on: ubuntu-latest - steps: - - name: Checkout the source code - uses: actions/checkout@v4 - - - name: Install Rust stable - run: | - rustup toolchain update --no-self-update stable - rustup default stable - rustup component add clippy rustfmt - - - name: Run rustfmt - run: cargo fmt -- --check - - - name: Run clippy - run: cargo clippy --all-features - env: - RUSTFLAGS: -D warnings - - test: - name: Test - runs-on: ${{ matrix.os }} - strategy: - matrix: - rust_versions: ["stable", "1.67"] - os: [ubuntu-latest, windows-latest] - steps: - - name: Checkout the source code - uses: actions/checkout@v4 - - - name: Install Rust Versions - run: | - rustup toolchain install --no-self-update ${{ matrix.rust_versions }} - rustup default stable - - - name: Build lib - run: rustup run ${{ matrix.rust_versions }} cargo build ${{ matrix.cargo_build_flags }} - - - name: test lib - run: cargo test --all-features - env: - RUSTFLAGS: -D warnings - - features: - name: Test Individual Features - runs-on: ubuntu-latest - steps: - - name: Checkout the source code - uses: actions/checkout@v4 - - - name: Install Rust toolchain and jq - run: | - rustup toolchain install --no-self-update stable - rustup default stable - sudo apt-get install -y --no-install-recommends jq - - - name: Test lib - run: | - for feature in $(cargo read-manifest | jq -r '.features|keys|join("\n")'); do - echo building with feature "$feature" - RUSTFLAGS='-D warnings' cargo test --no-default-features --features "$feature" - done - - bench: - name: Benchmark the background_rotation feature - runs-on: ubuntu-latest - strategy: - matrix: - rust_versions: ["stable", "1.67"] - steps: - - name: Checkout the source code - uses: actions/checkout@v4 - - - name: Install Rust toolchain - run: | - rustup toolchain install --no-self-update stable - rustup default stable - - - name: Rotation with backgrounded rotation - run: cargo bench --features gzip,background_rotation - - cargo-audit: - name: Audit the baseline for CVEs - runs-on: ubuntu-latest - steps: - - name: Checkout the source code - uses: actions/checkout@v4 - - - name: Install Rust toolchain - run: | - rustup toolchain install --no-self-update stable - rustup default stable - - - name: Install Auditting Tools - run: cargo install cargo-audit - - - name: Audit project - run: cargo audit - - docs-lint: - name: Lint - runs-on: ubuntu-latest - steps: - - name: Markdown Linting Action - uses: avto-dev/markdown-lint@v1.5.0 - with: - config: .markdownlint.yml - args: docs/Configuration.md README.md + test: + name: Test + runs-on: ${{ matrix.os }} + strategy: + matrix: + rust_versions: ["stable", "1.67"] + os: [ubuntu-latest, windows-latest] + steps: + - name: Checkout the source code + uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust_versions }} + + - name: Build lib + run: cargo build + + - name: Test lib + run: cargo test --all-features + env: + RUSTFLAGS: -D warnings + + features: + name: Test Individual Features + runs-on: ubuntu-latest + steps: + - name: Checkout the source code + uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@stable + + - name: Install jq + run: | + sudo apt-get install -y --no-install-recommends jq + + - name: Test lib + run: | + for feature in $(cargo read-manifest | jq -r '.features|keys|join("\n")'); do + echo building with feature "$feature" + RUSTFLAGS='-D warnings' cargo test --no-default-features --features "$feature" + done + + bench: + name: Benchmark the background_rotation feature + runs-on: ubuntu-latest + strategy: + matrix: + rust_versions: ["stable", "1.67"] + steps: + - name: Checkout the source code + uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust_versions }} + + - name: Bench features + run: cargo bench --all-features diff --git a/README.md b/README.md index 53219341..c3a9f3a8 100644 --- a/README.md +++ b/README.md @@ -31,25 +31,25 @@ log4rs.yaml: ```yaml refresh_rate: 30 seconds appenders: - stdout: - kind: console - requests: - kind: file - path: "log/requests.log" - encoder: - pattern: "{d} - {m}{n}" -root: - level: warn - appenders: - - stdout -loggers: - app::backend::db: - level: info - app::requests: - level: info - appenders: - - requests - additive: false + stdout: + kind: console + requests: + kind: file + path: "log/requests.log" + encoder: + pattern: "{d} - {m}{n}" + root: + level: warn + appenders: + - stdout + loggers: + app::backend::db: + level: info + app::requests: + level: info + appenders: + - requests + additive: false ``` lib.rs: diff --git a/docs/Configuration.md b/docs/Configuration.md index c0a8d8a6..d4120e5a 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -32,8 +32,8 @@ i.e. ```yml filters: - - kind: threshold - level: info + - kind: threshold + level: info ``` ### Encoder @@ -51,8 +51,8 @@ i.e. ```yml encoder: - kind: pattern - pattern: "{h({d(%+)(utc)} [{f}:{L}] {l:<6} {M}:{m})}{n}" + kind: pattern + pattern: "{h({d(%+)(utc)} [{f}:{L}] {l:<6} {M}:{m})}{n}" ``` ## Loggers @@ -76,11 +76,11 @@ i.e. ```yml loggers: - my_logger: - level: info - appenders: - - my_appender - additive: true + my_logger: + level: info + appenders: + - my_appender + additive: true ``` ## The Root Logger @@ -93,9 +93,9 @@ field does not apply. ```yml root: - level: info - appenders: - - my_appender + level: info + appenders: + - my_appender ``` ## Appenders @@ -121,9 +121,9 @@ the [encoder](#encoder) documention. ```yml my_console_appender: - kind: console - target: stdout - tty_only: false + kind: console + target: stdout + tty_only: false ``` #### The File Appender @@ -139,9 +139,9 @@ append to the log file if it exists, false will truncate the existing file. ```yml my_file_appender: - kind: file - path: $ENV{PWD}/log/test.log - append: true + kind: file + path: $ENV{PWD}/log/test.log + append: true ``` #### The Rolling File Appender @@ -154,18 +154,18 @@ i.e. ```yml my_rolling_appender: - kind: rolling_file - path: "logs/test.log" - policy: - kind: compound - trigger: - kind: size - limit: 1mb - roller: - kind: fixed_window - base: 1 - count: 5 - pattern: "logs/test.{}.log" + kind: rolling_file + path: "logs/test.log" + policy: + kind: compound + trigger: + kind: size + limit: 1mb + roller: + kind: fixed_window + base: 1 + count: 5 + pattern: "logs/test.{}.log" ``` The new component is the _policy_ field. A policy must have `kind` like most @@ -186,8 +186,8 @@ i.e. ```yml trigger: - kind: size - limit: 10 mb + kind: size + limit: 10 mb ``` The _roller_ field supports two types: delete, and fixed_window. The delete @@ -214,17 +214,17 @@ i.e. ```yml roller: - kind: fixed_window - base: 1 - count: 5 - pattern: "archive/journey-service.{}.log" + kind: fixed_window + base: 1 + count: 5 + pattern: "archive/journey-service.{}.log" ``` or ```yml roller: - kind: delete + kind: delete ``` ## Refresh Rate diff --git a/examples/sample_config.yml b/examples/sample_config.yml index 85e31936..4a0d69cd 100644 --- a/examples/sample_config.yml +++ b/examples/sample_config.yml @@ -1,12 +1,12 @@ appenders: - stdout: - kind: console - encoder: - pattern: "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" - filters: - - kind: threshold - level: info + stdout: + kind: console + encoder: + pattern: "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}" + filters: + - kind: threshold + level: info root: - level: info - appenders: - - stdout + level: info + appenders: + - stdout diff --git a/src/append/rolling_file/mod.rs b/src/append/rolling_file/mod.rs index 6f1f5072..e14e4ad8 100644 --- a/src/append/rolling_file/mod.rs +++ b/src/append/rolling_file/mod.rs @@ -366,28 +366,28 @@ mod test { let config = format!( " appenders: - foo: - kind: rolling_file - path: {0}/foo.log - policy: - trigger: - kind: size - limit: 1024 - roller: - kind: delete - bar: - kind: rolling_file - path: {0}/foo.log - policy: - kind: compound - trigger: - kind: size - limit: 5 mb - roller: - kind: fixed_window - pattern: '{0}/foo.log.{{}}' - base: 1 - count: 5 + foo: + kind: rolling_file + path: {0}/foo.log + policy: + trigger: + kind: size + limit: 1024 + roller: + kind: delete + bar: + kind: rolling_file + path: {0}/foo.log + policy: + kind: compound + trigger: + kind: size + limit: 5 mb + roller: + kind: fixed_window + pattern: '{0}/foo.log.{{}}' + base: 1 + count: 5 ", dir.path().display() ); diff --git a/src/config/raw.rs b/src/config/raw.rs index f04350f4..38645888 100644 --- a/src/config/raw.rs +++ b/src/config/raw.rs @@ -466,28 +466,28 @@ mod test { refresh_rate: 60 seconds appenders: - console: - kind: console - filters: - - kind: threshold - level: debug - baz: - kind: file - path: /tmp/baz.log - encoder: - pattern: "%m" + console: + kind: console + filters: + - kind: threshold + level: debug + baz: + kind: file + path: /tmp/baz.log + encoder: + pattern: "%m" root: - appenders: - - console - level: info + appenders: + - console + level: info loggers: - foo::bar::baz: - level: warn - appenders: - - baz - additive: false + foo::bar::baz: + level: warn + appenders: + - baz + additive: false "#; let config = ::serde_yaml::from_str::(cfg).unwrap(); let errors = config.appenders_lossy(&Deserializers::new()).1; diff --git a/src/encode/json.rs b/src/encode/json.rs index dc23fbdc..24c91119 100644 --- a/src/encode/json.rs +++ b/src/encode/json.rs @@ -207,8 +207,8 @@ mod test { let expected = format!( "{{\"time\":\"{}\",\"message\":\"{}\",\"module_path\":\"{}\",\ - \"file\":\"{}\",\"line\":{},\"level\":\"{}\",\"target\":\"{}\",\ - \"thread\":\"{}\",\"thread_id\":{},\"mdc\":{{\"foo\":\"bar\"}}}}", + \"file\":\"{}\",\"line\":{},\"level\":\"{}\",\"target\":\"{}\",\ + \"thread\":\"{}\",\"thread_id\":{},\"mdc\":{{\"foo\":\"bar\"}}}}", time.to_rfc3339(), message, module_path, diff --git a/src/encode/pattern/mod.rs b/src/encode/pattern/mod.rs index 9316ecc8..05dc432b 100644 --- a/src/encode/pattern/mod.rs +++ b/src/encode/pattern/mod.rs @@ -388,7 +388,7 @@ impl<'a> From> for Chunk { return Chunk::Error("expected at most two arguments".to_owned()); } - let format = match formatter.args.get(0) { + let format = match formatter.args.first() { Some(arg) => { let mut format = String::new(); for piece in arg { @@ -411,7 +411,7 @@ impl<'a> From> for Chunk { let timezone = match formatter.args.get(1) { Some(arg) => { - if let Some(arg) = arg.get(0) { + if let Some(arg) = arg.first() { match *arg { Piece::Text("utc") => Timezone::Utc, Piece::Text("local") => Timezone::Local, @@ -465,9 +465,9 @@ impl<'a> From> for Chunk { return Chunk::Error("expected at most two arguments".to_owned()); } - let key = match formatter.args.get(0) { + let key = match formatter.args.first() { Some(arg) => { - if let Some(arg) = arg.get(0) { + if let Some(arg) = arg.first() { match arg { Piece::Text(key) => key.to_owned(), Piece::Error(ref e) => return Chunk::Error(e.clone()), @@ -482,7 +482,7 @@ impl<'a> From> for Chunk { let default = match formatter.args.get(1) { Some(arg) => { - if let Some(arg) = arg.get(0) { + if let Some(arg) = arg.first() { match arg { Piece::Text(key) => key.to_owned(), Piece::Error(ref e) => return Chunk::Error(e.clone()), diff --git a/test/log.yml b/test/log.yml index b8c5aee3..f51edcc1 100644 --- a/test/log.yml +++ b/test/log.yml @@ -6,8 +6,8 @@ appenders: encoder: pattern: "{d(%+)(local)} [{t}] {h({l})} {M}:{m}{n}" filters: - - kind: threshold - level: error + - kind: threshold + level: error file: kind: file path: error.log