Skip to content

Commit c05531b

Browse files
committed
Use start a fresh daemon for tests.
1 parent b9683ac commit c05531b

File tree

5 files changed

+145
-11
lines changed

5 files changed

+145
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ rustc-serialize = "0.3.16"
1414
time = "0.1.34"
1515

1616
[dev-dependencies]
17+
tempdir = "0.3.5"
1718
unix_socket = "0.5.0"

tests/helpers.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/helpers/daemon.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
extern crate tempdir;
2+
3+
use self::tempdir::TempDir;
4+
use super::mpd;
5+
use std::fs::{File, create_dir};
6+
use std::io::Write;
7+
use std::os::unix::net::UnixStream;
8+
use std::path::{Path, PathBuf};
9+
use std::process::{Command, Child};
10+
11+
struct MpdConfig {
12+
db_file: PathBuf,
13+
music_directory: PathBuf,
14+
playlist_directory: PathBuf,
15+
config_path: PathBuf,
16+
sock_path: PathBuf,
17+
}
18+
19+
impl MpdConfig {
20+
pub fn new<P>(base: P) -> MpdConfig
21+
where P: AsRef<Path>
22+
{
23+
let base = base.as_ref();
24+
MpdConfig {
25+
db_file: base.join("db"),
26+
music_directory: base.join("music"),
27+
playlist_directory: base.join("playlists"),
28+
config_path: base.join("config"),
29+
sock_path: base.join("sock"),
30+
}
31+
}
32+
}
33+
34+
fn write_config(config: &MpdConfig) {
35+
let config_text = format!(r#"
36+
db_file "{db_file}"
37+
log_file "/dev/null"
38+
music_directory "{music_directory}"
39+
playlist_directory "{playlist_directory}"
40+
bind_to_address "{sock_path}"
41+
audio_output {{
42+
type "null"
43+
name "null"
44+
}}
45+
"#,
46+
db_file=config.db_file.display(),
47+
music_directory=config.music_directory.display(),
48+
playlist_directory=config.playlist_directory.display(),
49+
sock_path=config.sock_path.display(),
50+
);
51+
create_dir(&config.music_directory).expect("Could not create music directory.");
52+
create_dir(&config.playlist_directory).expect("Could not create playlist directory.");
53+
let mut file = File::create(&config.config_path).expect("Could not create config file.");
54+
file.write_all(config_text.as_bytes()).expect("Could not write config file.");
55+
}
56+
57+
pub struct Daemon {
58+
// Saved here so it gets dropped when this does.
59+
_temp_dir: TempDir,
60+
config: MpdConfig,
61+
process: Child,
62+
}
63+
64+
impl Drop for Daemon {
65+
fn drop(&mut self) {
66+
self.process.kill().expect("Could not kill mpd daemon.");
67+
self.process.wait().expect("Could not wait for mpd daemon to shutdown.");
68+
}
69+
}
70+
71+
fn sleep() {
72+
use std::{thread, time};
73+
let ten_millis = time::Duration::from_millis(100);
74+
thread::sleep(ten_millis);
75+
}
76+
77+
impl Daemon {
78+
#[cfg_attr(feature="cargo-clippy", allow(new_without_default))]
79+
pub fn new() -> Daemon {
80+
let temp_dir = TempDir::new("mpd-test").unwrap();
81+
let config = MpdConfig::new(&temp_dir);
82+
write_config(&config);
83+
let process = Command::new("mpd")
84+
.arg("--no-daemon")
85+
.arg(&config.config_path)
86+
.spawn()
87+
.expect("Could not create mpd daemon.");
88+
while !config.sock_path.exists() {}
89+
90+
// FIXME: Wait for mpd to finish updating the database.
91+
sleep();
92+
93+
Daemon {
94+
_temp_dir: temp_dir,
95+
config: config,
96+
process: process,
97+
}
98+
}
99+
100+
pub fn connect(&self) -> mpd::Client<UnixStream> {
101+
let stream = UnixStream::connect(&self.config.sock_path).unwrap();
102+
mpd::Client::new(stream).unwrap()
103+
}
104+
}

tests/helpers/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extern crate mpd;
2+
3+
mod daemon;
4+
5+
pub use self::daemon::Daemon;
6+
use std::os::unix::net::UnixStream;
7+
8+
pub struct DaemonClient {
9+
_daemon: Daemon,
10+
client: mpd::Client<UnixStream>,
11+
}
12+
13+
use std::ops::{Deref, DerefMut};
14+
15+
impl Deref for DaemonClient {
16+
type Target = mpd::Client<UnixStream>;
17+
fn deref(&self) -> &Self::Target {
18+
&self.client
19+
}
20+
}
21+
22+
impl DerefMut for DaemonClient {
23+
fn deref_mut(&mut self) -> &mut Self::Target {
24+
&mut self.client
25+
}
26+
}
27+
28+
#[allow(dead_code)]
29+
pub fn connect() -> DaemonClient {
30+
let daemon = Daemon::start();
31+
let client = daemon.connect();
32+
DaemonClient {
33+
_daemon: daemon,
34+
client: client,
35+
}
36+
}

tests/idle.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
extern crate mpd;
22

33
mod helpers;
4+
use helpers::Daemon;
45

56
use mpd::Idle;
67

78
#[test]
89
fn idle() {
9-
let mut mpd = helpers::connect();
10+
let daemon = Daemon::new();
11+
let mut mpd = daemon.connect();
1012
let idle = mpd.idle(&[]).unwrap();
1113

12-
let mut mpd1 = helpers::connect();
14+
let mut mpd1 = daemon.connect();
1315
mpd1.consume(true).unwrap();
1416
mpd1.consume(false).unwrap();
1517

0 commit comments

Comments
 (0)