summaryrefslogtreecommitdiffstats
path: root/src/tools/cargo/tests/testsuite/death.rs
blob: b61896dc917b6a6928493e011227b80c3f6347a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Tests for ctrl-C handling.

use cargo_test_support::{project, slow_cpu_multiplier};
use std::fs;
use std::io::{self, Read};
use std::net::TcpListener;
use std::process::{Child, Stdio};
use std::thread;
use std::time;

#[cargo_test]
fn ctrl_c_kills_everyone() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();

    let p = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name = "foo"
                version = "0.0.1"
                authors = []
                build = "build.rs"
            "#,
        )
        .file("src/lib.rs", "")
        .file(
            "build.rs",
            &format!(
                r#"
                    use std::net::TcpStream;
                    use std::io::Read;

                    fn main() {{
                        let mut socket = TcpStream::connect("{}").unwrap();
                        let _ = socket.read(&mut [0; 10]);
                        panic!("that read should never return");
                    }}
                "#,
                addr
            ),
        )
        .build();

    let mut cargo = p.cargo("check").build_command();
    cargo
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .env("__CARGO_TEST_SETSID_PLEASE_DONT_USE_ELSEWHERE", "1");
    let mut child = cargo.spawn().unwrap();

    let mut sock = listener.accept().unwrap().0;
    ctrl_c(&mut child);

    assert!(!child.wait().unwrap().success());
    match sock.read(&mut [0; 10]) {
        Ok(n) => assert_eq!(n, 0),
        Err(e) => assert_eq!(e.kind(), io::ErrorKind::ConnectionReset),
    }

    // Ok so what we just did was spawn cargo that spawned a build script, then
    // we killed cargo in hopes of it killing the build script as well. If all
    // went well the build script is now dead. On Windows, however, this is
    // enforced with job objects which means that it may actually be in the
    // *process* of being torn down at this point.
    //
    // Now on Windows we can't completely remove a file until all handles to it
    // have been closed. Including those that represent running processes. So if
    // we were to return here then there may still be an open reference to some
    // file in the build directory. What we want to actually do is wait for the
    // build script to *complete* exit. Take care of that by blowing away the
    // build directory here, and panicking if we eventually spin too long
    // without being able to.
    for i in 0..10 {
        match fs::remove_dir_all(&p.root().join("target")) {
            Ok(()) => return,
            Err(e) => println!("attempt {}: {}", i, e),
        }
        thread::sleep(slow_cpu_multiplier(100));
    }

    panic!(
        "couldn't remove build directory after a few tries, seems like \
         we won't be able to!"
    );
}

#[cargo_test]
fn kill_cargo_add_never_corrupts_cargo_toml() {
    cargo_test_support::registry::init();
    cargo_test_support::registry::Package::new("my-package", "0.1.1+my-package").publish();

    let with_dependency = r#"
[package]
name = "foo"
version = "0.0.1"
authors = []

[dependencies]
my-package = "0.1.1"
"#;
    let without_dependency = r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#;

    for sleep_time_ms in [30, 60, 90] {
        let p = project()
            .file("Cargo.toml", without_dependency)
            .file("src/lib.rs", "")
            .build();

        let mut cargo = p.cargo("add").arg("my-package").build_command();
        cargo
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let mut child = cargo.spawn().unwrap();

        thread::sleep(time::Duration::from_millis(sleep_time_ms));

        assert!(child.kill().is_ok());
        assert!(child.wait().is_ok());

        // check the Cargo.toml
        let contents = fs::read(p.root().join("Cargo.toml")).unwrap();

        // not empty
        assert_ne!(
            contents, b"",
            "Cargo.toml is empty, and should not be at {} milliseconds",
            sleep_time_ms
        );

        // We should have the original Cargo.toml or the new one, nothing else.
        if std::str::from_utf8(&contents)
            .unwrap()
            .contains("[dependencies]")
        {
            assert_eq!(
                std::str::from_utf8(&contents).unwrap(),
                with_dependency,
                "Cargo.toml is with_dependency after add at {} milliseconds",
                sleep_time_ms
            );
        } else {
            assert_eq!(
                std::str::from_utf8(&contents).unwrap(),
                without_dependency,
                "Cargo.toml is without_dependency after add at {} milliseconds",
                sleep_time_ms
            );
        }
    }
}

#[cargo_test]
fn kill_cargo_remove_never_corrupts_cargo_toml() {
    let with_dependency = r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"

[dependencies]
bar = "0.0.1"
"#;
    let without_dependency = r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
build = "build.rs"
"#;

    // This test depends on killing the cargo process at the right time to cause a failed write.
    // Note that we're iterating and using the index as time in ms to sleep before killing the cargo process.
    // If it is working correctly, we never fail, but can't hang out here all day...
    // So we'll just run it a few times and hope for the best.
    for sleep_time_ms in [30, 60, 90] {
        // new basic project with a single dependency
        let p = project()
            .file("Cargo.toml", with_dependency)
            .file("src/lib.rs", "")
            .build();

        // run cargo remove the dependency
        let mut cargo = p.cargo("remove").arg("bar").build_command();
        cargo
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let mut child = cargo.spawn().unwrap();

        thread::sleep(time::Duration::from_millis(sleep_time_ms));

        assert!(child.kill().is_ok());
        assert!(child.wait().is_ok());

        // check the Cargo.toml
        let contents = fs::read(p.root().join("Cargo.toml")).unwrap();

        // not empty
        assert_ne!(
            contents, b"",
            "Cargo.toml is empty, and should not be at {} milliseconds",
            sleep_time_ms
        );

        // We should have the original Cargo.toml or the new one, nothing else.
        if std::str::from_utf8(&contents)
            .unwrap()
            .contains("[dependencies]")
        {
            assert_eq!(
                std::str::from_utf8(&contents).unwrap(),
                with_dependency,
                "Cargo.toml is not the same as the original at {} milliseconds",
                sleep_time_ms
            );
        } else {
            assert_eq!(
                std::str::from_utf8(&contents).unwrap(),
                without_dependency,
                "Cargo.toml is not the same as expected at {} milliseconds",
                sleep_time_ms
            );
        }
    }
}

#[cfg(unix)]
pub fn ctrl_c(child: &mut Child) {
    let r = unsafe { libc::kill(-(child.id() as i32), libc::SIGINT) };
    if r < 0 {
        panic!("failed to kill: {}", io::Error::last_os_error());
    }
}

#[cfg(windows)]
pub fn ctrl_c(child: &mut Child) {
    child.kill().unwrap();
}