1
1
mod full {
2
+ use log:: { log_enabled, Level } ;
2
3
use std:: {
3
4
env,
4
5
fs:: File ,
5
- path:: { Path , PathBuf } ,
6
+ io:: { BufRead , Write } ,
7
+ path:: Path ,
6
8
process:: { Command , Stdio } ,
7
9
} ;
8
10
9
11
fn test_full ( crate_name : & str , old_version : & str , new_version : & str , expected_result : bool ) {
10
- let prog = format ! (
11
- r#"
12
- # wait for the actual output
13
- /^version bump/ {{
14
- doprint = 1;
15
- }}
12
+ // Add target dir to PATH so cargo-semver will call the right rust-semverver
13
+ if let Some ( path) = env:: var_os ( "PATH" ) {
14
+ let mut paths = env:: split_paths ( & path) . collect :: < Vec < _ > > ( ) ;
15
+ let current_dir = env:: current_dir ( ) . expect ( "could not determine current dir" ) ;
16
+ paths. insert ( 0 , current_dir. join ( "target/debug" ) ) ;
17
+ let new_path = env:: join_paths ( paths) . unwrap ( ) ;
18
+ env:: set_var ( "PATH" , & new_path) ;
19
+ } else {
20
+ eprintln ! ( "no path!" ) ;
21
+ }
16
22
17
- {{
18
- # check the environ for filtering
19
- if (ENVIRON["RUST_LOG"] == "debug")
20
- doprint = 1;
23
+ let mut cmd = Command :: new ( "./target/debug/cargo-semver" ) ;
24
+ cmd. args ( & [
25
+ "-S" , & format ! ( "{}:{}" , crate_name, old_version) ,
26
+ "-C" , & format ! ( "{}:{}" , crate_name, new_version) ,
27
+ "-q"
28
+ ] )
29
+ . env ( "RUST_BACKTRACE" , "full" )
30
+ . stdin ( Stdio :: null ( ) )
31
+ . stdout ( Stdio :: piped ( ) )
32
+ . stderr ( Stdio :: piped ( ) ) ;
33
+
34
+ if let Ok ( target) = std:: env:: var ( "TEST_TARGET" ) {
35
+ cmd. args ( & [ "--target" , & target] ) ;
36
+ }
21
37
22
- # skip compilation info
23
- if (!doprint)
24
- next;
38
+ let output = cmd. output ( ) . expect ( "could not run cargo semver" ) ;
25
39
26
- # sanitize paths
27
- gsub(/-->.*{crate_name}/, "--> {crate_name}", $0);
28
- print;
29
- }}{crate_name}{crate_name}"# ,
30
- crate_name = crate_name
40
+ assert_eq ! ( output. status. success( ) , expected_result,
41
+ "cargo-semver returned unexpected exit status {}" , output. status
31
42
) ;
32
43
33
- let out_file = Path :: new ( "tests/full_cases" )
34
- . join ( format ! ( "{}-{}-{}" , crate_name, old_version, new_version) ) ;
35
-
36
44
// Choose solution depending on the platform
37
45
let file_ext = if cfg ! ( target_os = "macos" ) {
38
46
"osx"
@@ -45,82 +53,40 @@ mod full {
45
53
return ;
46
54
} ;
47
55
48
- let out_file: PathBuf = format ! ( "{}.{}" , out_file. display( ) , file_ext) . into ( ) ;
49
- assert ! (
50
- out_file. exists( ) ,
51
- "file `{}` does not exist" ,
52
- out_file. display( )
53
- ) ;
54
-
55
- if let Some ( path) = env:: var_os ( "PATH" ) {
56
- let mut paths = env:: split_paths ( & path) . collect :: < Vec < _ > > ( ) ;
57
- let current_dir = env:: current_dir ( ) . expect ( "could not determine current dir" ) ;
58
- paths. insert ( 0 , current_dir. join ( "target/debug" ) ) ;
59
- let new_path = env:: join_paths ( paths) . unwrap ( ) ;
60
- env:: set_var ( "PATH" , & new_path) ;
61
- } else {
62
- eprintln ! ( "no path!" ) ;
56
+ let filename = Path :: new ( "tests/full_cases" ) . join ( format ! (
57
+ "{}-{}-{}.{}" ,
58
+ crate_name, old_version, new_version, file_ext
59
+ ) ) ;
60
+
61
+ assert ! ( filename. exists( ) , "file `{}` does not exist" , filename. display( ) ) ;
62
+
63
+ let mut file = File :: create ( & filename) . expect ( "could not create output file" ) ;
64
+
65
+ for line in output. stdout . lines ( )
66
+ . chain ( output. stderr . lines ( ) )
67
+ . map ( |r| r. expect ( "could not read line from cargo-semver output" ) )
68
+ . skip_while ( |line|
69
+ // skip everything before the first important bit of info
70
+ !line. starts_with ( "version bump" ) &&
71
+ // ...unless debugging is enabled
72
+ !log_enabled ! ( Level :: Debug ) )
73
+ {
74
+ // sanitize paths for reproducibility
75
+ let output = match line. find ( "-->" ) {
76
+ Some ( idx) => {
77
+ let ( start, end) = line. split_at ( idx) ;
78
+ match end. find ( crate_name) {
79
+ Some ( idx) => format ! ( "{}--> {}" , start, end. split_at( idx) . 1 ) ,
80
+ None => line,
81
+ }
82
+ }
83
+ None => line,
84
+ } ;
85
+ writeln ! ( file, "{}" , output) . expect ( "error writing to output file" ) ;
63
86
}
64
87
65
- let stdout = File :: create ( & out_file) . expect ( "could not create `stdout` file" ) ;
66
- let out_file = out_file. to_str ( ) . unwrap ( ) ;
67
-
68
- let mut awk_child = Command :: new ( "awk" )
69
- . arg ( & prog)
70
- . stdin ( Stdio :: piped ( ) )
71
- . stdout ( stdout)
72
- . spawn ( )
73
- . expect ( "could not run awk" ) ;
74
-
75
- let ( err_pipe, out_pipe) = if let Some ( ref stdin) = awk_child. stdin {
76
- #[ cfg( unix) ]
77
- {
78
- use std:: os:: unix:: io:: { AsRawFd , FromRawFd } ;
79
- let fd = stdin. as_raw_fd ( ) ;
80
- unsafe { ( Stdio :: from_raw_fd ( fd) , Stdio :: from_raw_fd ( fd) ) }
81
- }
82
- #[ cfg( windows) ]
83
- {
84
- use std:: os:: windows:: io:: { AsRawHandle , FromRawHandle } ;
85
- let fd = stdin. as_raw_handle ( ) ;
86
- unsafe { ( Stdio :: from_raw_handle ( fd) , Stdio :: from_raw_handle ( fd) ) }
87
- }
88
- } else {
89
- panic ! ( "could not pipe to awk" ) ;
90
- } ;
91
-
92
- let old_version = format ! ( "{}:{}" , crate_name, old_version) ;
93
- let new_version = format ! ( "{}:{}" , crate_name, new_version) ;
94
-
95
- let cargo_semver_result = {
96
- let mut cmd = Command :: new ( "./target/debug/cargo-semver" ) ;
97
- cmd. args ( & [ "-S" , & old_version, "-C" , & new_version] )
98
- . env ( "RUST_BACKTRACE" , "full" )
99
- . stdin ( Stdio :: null ( ) )
100
- . stdout ( out_pipe)
101
- . stderr ( err_pipe) ;
102
-
103
- if let Ok ( target) = std:: env:: var ( "TEST_TARGET" ) {
104
- cmd. args ( & [ "--target" , & target] ) ;
105
- }
106
-
107
- cmd. status ( ) . expect ( "could not run cargo semver" ) . success ( )
108
- } ;
109
-
110
- assert_eq ! (
111
- cargo_semver_result, expected_result,
112
- "cargo semver returned an unexpected exit status"
113
- ) ;
114
-
115
- let awk_result = awk_child
116
- . wait ( )
117
- . expect ( "could not wait for awk child" )
118
- . success ( ) ;
119
-
120
- assert ! ( awk_result, "awk" ) ;
121
-
122
88
let git_result = Command :: new ( "git" )
123
- . args ( & [ "diff" , "--ignore-space-at-eol" , "--exit-code" , out_file ] )
89
+ . args ( & [ "diff" , "--ignore-space-at-eol" , "--exit-code" , filename . to_str ( ) . unwrap ( ) ] )
124
90
. env ( "PAGER" , "" )
125
91
. status ( )
126
92
. expect ( "could not run git diff" )
0 commit comments