Skip to content

Commit 8451e00

Browse files
valethsds
authored andcommitted
Add CargoTest pre-push hook
Runs tests via `cargo test` for Rust projects.
1 parent c96cf0b commit 8451e00

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

config/default.yml

+7
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,13 @@ PrePush:
11171117
flags: ['--exit-on-warn', '--quiet', '--summary']
11181118
install_command: 'gem install brakeman'
11191119

1120+
CargoTest:
1121+
enabled: false
1122+
description: 'Run tests with cargo'
1123+
required_executable: 'cargo'
1124+
flags: ['test']
1125+
include: 'src/**/*.rs'
1126+
11201127
GitLfs:
11211128
enabled: false
11221129
description: 'Upload files tracked by Git LFS'
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Overcommit::Hook::PrePush
2+
# Runs `cargo test` before push if Rust files changed
3+
class CargoTest < Base
4+
def run
5+
result = execute(command)
6+
return :pass if result.success?
7+
[:fail, result.stdout]
8+
end
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PrePush::CargoTest do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context') }
6+
subject { described_class.new(config, context) }
7+
8+
context 'when all tests succeed' do
9+
before do
10+
result = double('result')
11+
result.stub(:success?).and_return(true)
12+
subject.stub(:execute).and_return(result)
13+
end
14+
15+
it { should pass }
16+
end
17+
18+
context 'when one test fails' do
19+
before do
20+
result = double('result')
21+
result.stub(:success?).and_return(false)
22+
result.stub(stdout: <<-ERRORMSG)
23+
running 2 tests
24+
test tests::foo ... ok
25+
test tests::bar ... FAILED
26+
27+
failures:
28+
29+
---- tests::bar stdout ----
30+
thread 'tests::bar' panicked at 'assertion failed: `(left == right)`
31+
left: `None`,
32+
right: `Some(Bar)`', src/foobar.rs:88:9
33+
note: Run with `RUST_BACKTRACE=1` for a backtrace.
34+
35+
36+
failures:
37+
tests::bar
38+
39+
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
40+
ERRORMSG
41+
subject.stub(:execute).and_return(result)
42+
end
43+
44+
it { should fail_hook }
45+
end
46+
end

0 commit comments

Comments
 (0)