-
Notifications
You must be signed in to change notification settings - Fork 48
/
command.rs
232 lines (205 loc) · 10.3 KB
/
command.rs
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
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
#[test]
#[cfg(all(feature = "lzma", target_os = "linux"))]
fn run_cargo_deb_command_on_example_dir() {
let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let cmd_path = root.join("target/debug/cargo-deb");
assert!(cmd_path.exists());
let output = Command::new(cmd_path)
.arg(format!("--manifest-path={}", root.join("example/Cargo.toml").display()))
.output().unwrap();
if !output.status.success() {
panic!("Cmd failed: {}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
}
// prints deb path on the last line
let last_line = output.stdout[..output.stdout.len() - 1].split(|&c| c == b'\n').last().unwrap();
let deb_path = Path::new(::std::str::from_utf8(last_line).unwrap());
assert!(deb_path.exists());
let ardir = tempfile::tempdir().expect("testdir");
assert!(ardir.path().exists());
assert!(Command::new("ar")
.current_dir(ardir.path())
.arg("-x")
.arg(deb_path)
.status().unwrap().success());
assert_eq!("2.0\n", fs::read_to_string(ardir.path().join("debian-binary")).unwrap());
assert!(ardir.path().join("data.tar.xz").exists());
assert!(ardir.path().join("control.tar.xz").exists());
let cdir = tempfile::tempdir().unwrap();
assert!(Command::new("tar")
.arg("xf")
.current_dir(cdir.path())
.arg(ardir.path().join("control.tar.xz"))
.status().unwrap().success());
let control = fs::read_to_string(cdir.path().join("control")).unwrap();
assert!(control.contains("Package: example\n"));
assert!(control.contains("Version: 0.1.0\n"));
assert!(control.contains("Section: utils\n"));
assert!(control.contains("Architecture: "));
assert!(control.contains("Maintainer: cargo-deb developers <cargo-deb@example.invalid>\n"));
let md5sums = fs::read_to_string(cdir.path().join("md5sums")).unwrap();
assert!(md5sums.contains(" usr/bin/example\n"));
assert!(md5sums.contains(" usr/share/doc/example/changelog.Debian.gz\n"));
assert!(md5sums.contains("b1946ac92492d2347c6235b4d2611184 var/lib/example/1.txt\n"));
assert!(md5sums.contains("591785b794601e212b260e25925636fd var/lib/example/2.txt\n"));
assert!(md5sums.contains("1537684900f6b12358c88a612adf1049 var/lib/example/3.txt\n"));
assert!(md5sums.contains("6f65f1e8907ea8a25171915b3bba45af usr/share/doc/example/copyright\n"));
let ddir = tempfile::tempdir().unwrap();
assert!(Command::new("tar")
.arg("xJf")
.current_dir(ddir.path())
.arg(ardir.path().join("data.tar.xz"))
.status().unwrap().success());
assert!(ddir.path().join("var/lib/example/1.txt").exists());
assert!(ddir.path().join("var/lib/example/2.txt").exists());
assert!(ddir.path().join("var/lib/example/3.txt").exists());
assert!(ddir.path().join("usr/share/doc/example/copyright").exists());
assert!(ddir.path().join("usr/share/doc/example/changelog.Debian.gz").exists());
assert!(ddir.path().join("usr/bin/example").exists());
// changelog.Debian.gz starts with the gzip magic
assert_eq!(
&[0x1F, 0x8B],
&fs::read(ddir.path().join("usr/share/doc/example/changelog.Debian.gz")).unwrap()[..2]
);
}
#[test]
#[cfg(all(feature = "lzma"))]
fn run_cargo_deb_command_on_example_dir_with_variant() {
let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let cmd_path = root.join(format!("target/debug/cargo-deb{}", std::env::consts::EXE_SUFFIX));
assert!(cmd_path.exists());
let cargo_dir = tempfile::tempdir().unwrap();
let deb_path = cargo_dir.path().join("test.deb");
let output = Command::new(cmd_path)
.env("CARGO_TARGET_DIR", cargo_dir.path()) // otherwise tests overwrite each other
.arg("--variant=debug")
.arg("--no-strip")
.arg(format!("--output={}", deb_path.display()))
.arg(format!(
"--manifest-path={}",
root.join("example/Cargo.toml").display()
))
.output()
.unwrap();
if !output.status.success() {
panic!("Cmd failed: {}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
}
// prints deb path on the last line
let last_line = output.stdout[..output.stdout.len()-1].split(|&c| c==b'\n').last().unwrap();
let printed_deb_path = Path::new(::std::str::from_utf8(last_line).unwrap());
assert_eq!(printed_deb_path, deb_path);
assert!(deb_path.exists());
let ardir = tempfile::tempdir().unwrap();
assert!(ardir.path().exists());
assert!(Command::new("ar")
.current_dir(ardir.path())
.arg("-x")
.arg(deb_path)
.status().unwrap().success());
assert_eq!("2.0\n", fs::read_to_string(ardir.path().join("debian-binary")).unwrap());
assert!(ardir.path().join("data.tar.xz").exists());
assert!(ardir.path().join("control.tar.xz").exists());
let cdir = tempfile::tempdir().unwrap();
assert!(Command::new("tar")
.arg("xJf")
.current_dir(cdir.path())
.arg(ardir.path().join("control.tar.xz"))
.status().unwrap().success());
let control = fs::read_to_string(cdir.path().join("control")).unwrap();
assert!(control.contains("Package: example-debug\n"), "Control is: {:?}", control);
assert!(control.contains("Version: 0.1.0\n"));
assert!(control.contains("Section: utils\n"));
assert!(control.contains("Architecture: "));
assert!(control.contains("Maintainer: cargo-deb developers <cargo-deb@example.invalid>\n"));
let md5sums = fs::read_to_string(cdir.path().join("md5sums")).unwrap();
assert!(md5sums.contains(" usr/bin/example\n"));
assert!(md5sums.contains(" usr/share/doc/example-debug/changelog.Debian.gz\n"));
assert!(md5sums.contains("b1946ac92492d2347c6235b4d2611184 var/lib/example/1.txt\n"));
assert!(md5sums.contains("591785b794601e212b260e25925636fd var/lib/example/2.txt\n"));
assert!(md5sums.contains("835a3c46f2330925774ebf780aa74241 var/lib/example/4.txt\n"));
assert!(md5sums.contains("2455967cef930e647146a8c762199ed3 usr/share/doc/example-debug/copyright\n"));
let ddir = tempfile::tempdir().unwrap();
assert!(Command::new("tar")
.arg("xJf")
.current_dir(ddir.path())
.arg(ardir.path().join("data.tar.xz"))
.status().unwrap().success());
assert!(ddir.path().join("var/lib/example/1.txt").exists());
assert!(ddir.path().join("var/lib/example/2.txt").exists());
assert!(ddir.path().join("var/lib/example/4.txt").exists());
assert!(ddir.path().join("usr/share/doc/example-debug/copyright").exists());
assert!(ddir.path().join("usr/share/doc/example-debug/changelog.Debian.gz").exists());
assert!(ddir.path().join("usr/bin/example").exists());
}
#[test]
#[cfg(all(feature = "lzma", target_os = "linux"))]
fn run_cargo_deb_command_on_example_dir_with_version() {
let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let cmd_path = root.join("target/debug/cargo-deb");
assert!(cmd_path.exists());
let cargo_dir = tempfile::tempdir().unwrap();
let deb_path = cargo_dir.path().join("test.deb");
let output = Command::new(cmd_path)
.env("CARGO_TARGET_DIR", cargo_dir.path()) // otherwise tests overwrite each other
.arg(format!("--output={}", deb_path.display()))
.arg(format!(
"--manifest-path={}",
root.join("example/Cargo.toml").display()
))
.arg(format!("--deb-version=my-custom-version"))
.output().unwrap();
assert!(output.status.success());
// prints deb path on the last line
let last_line = output.stdout[..output.stdout.len() - 1].split(|&c| c == b'\n').last().unwrap();
let deb_path = Path::new(::std::str::from_utf8(last_line).unwrap());
assert!(deb_path.exists());
let ardir = tempfile::tempdir().unwrap();
assert!(ardir.path().exists());
assert!(Command::new("ar")
.current_dir(ardir.path())
.arg("-x")
.arg(deb_path)
.status().unwrap().success());
assert_eq!("2.0\n", fs::read_to_string(ardir.path().join("debian-binary")).unwrap());
assert!(ardir.path().join("data.tar.xz").exists());
assert!(ardir.path().join("control.tar.xz").exists());
let cdir = tempfile::tempdir().unwrap();
assert!(Command::new("tar")
.arg("xf")
.current_dir(cdir.path())
.arg(ardir.path().join("control.tar.xz"))
.status().unwrap().success());
let control = fs::read_to_string(cdir.path().join("control")).unwrap();
assert!(control.contains("Package: example\n"));
assert!(control.contains("Version: my-custom-version\n"));
assert!(control.contains("Section: utils\n"));
assert!(control.contains("Architecture: "));
assert!(control.contains("Maintainer: cargo-deb developers <cargo-deb@example.invalid>\n"));
let md5sums = fs::read_to_string(cdir.path().join("md5sums")).unwrap();
assert!(md5sums.contains(" usr/bin/example\n"));
assert!(md5sums.contains(" usr/share/doc/example/changelog.Debian.gz\n"));
assert!(md5sums.contains("b1946ac92492d2347c6235b4d2611184 var/lib/example/1.txt\n"));
assert!(md5sums.contains("591785b794601e212b260e25925636fd var/lib/example/2.txt\n"));
assert!(md5sums.contains("1537684900f6b12358c88a612adf1049 var/lib/example/3.txt\n"));
assert!(md5sums.contains("6f65f1e8907ea8a25171915b3bba45af usr/share/doc/example/copyright\n"), "has:\n{}", md5sums);
let ddir = tempfile::tempdir().unwrap();
assert!(Command::new("tar")
.arg("xJf")
.current_dir(ddir.path())
.arg(ardir.path().join("data.tar.xz"))
.status().unwrap().success());
assert!(ddir.path().join("var/lib/example/1.txt").exists());
assert!(ddir.path().join("var/lib/example/2.txt").exists());
assert!(ddir.path().join("var/lib/example/3.txt").exists());
assert!(ddir.path().join("usr/share/doc/example/copyright").exists());
assert!(ddir.path().join("usr/share/doc/example/changelog.Debian.gz").exists());
assert!(ddir.path().join("usr/bin/example").exists());
// changelog.Debian.gz starts with the gzip magic
assert_eq!(
&[0x1F, 0x8B],
&fs::read(ddir.path().join("usr/share/doc/example/changelog.Debian.gz")).unwrap()[..2]
);
}