File: lib.rs

package info (click to toggle)
rust-atomicwrites 0.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 120 kB
  • sloc: makefile: 4
file content (104 lines) | stat: -rw-r--r-- 2,993 bytes parent folder | download
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
extern crate atomicwrites;
extern crate tempfile;

use atomicwrites::{AllowOverwrite, AtomicFile, DisallowOverwrite};
use std::io::{self, Read, Write};
use std::{env, fs, path};
use tempfile::TempDir;

fn get_tmp() -> path::PathBuf {
    TempDir::new().unwrap().into_path()
}

#[test]
fn test_simple_allow_override() {
    let tmpdir = get_tmp();
    let path = tmpdir.join("haha");

    let af = AtomicFile::new(&path, AllowOverwrite);
    let res: io::Result<()> = af.write(|f| f.write_all(b"HELLO")).map_err(|x| x.into());
    res.unwrap();
    af.write(|f| f.write_all(b"HELLO")).unwrap();

    let mut rv = String::new();
    let mut testfd = fs::File::open(&path).unwrap();
    testfd.read_to_string(&mut rv).unwrap();
    assert_eq!(&rv[..], "HELLO");
}

#[test]
fn test_simple_disallow_override() {
    let tmpdir = get_tmp();
    let path = tmpdir.join("haha");

    let af = AtomicFile::new(&path, DisallowOverwrite);
    af.write(|f| f.write_all(b"HELLO")).unwrap();
    assert!(af.write(|f| f.write_all(b"HELLO")).is_err());

    let mut rv = String::new();
    let mut testfd = fs::File::open(&path).unwrap();
    testfd.read_to_string(&mut rv).unwrap();
    assert_eq!(&rv[..], "HELLO");
}

#[test]
fn test_allowed_pathtypes() {
    AtomicFile::new("haha", DisallowOverwrite);
    AtomicFile::new(&"haha", DisallowOverwrite);
    AtomicFile::new(&path::Path::new("haha"), DisallowOverwrite);
    AtomicFile::new(&path::PathBuf::from("haha"), DisallowOverwrite);
}

#[test]
fn test_unicode() {
    let dmitri = "Дмитрий";
    let greeting = format!("HELLO {}", dmitri);

    let tmpdir = get_tmp();
    let path = tmpdir.join(dmitri);

    let af = AtomicFile::new(&path, DisallowOverwrite);
    af.write(|f| f.write_all(greeting.as_bytes())).unwrap();

    let mut rv = String::new();
    let mut testfd = fs::File::open(&path).unwrap();
    testfd.read_to_string(&mut rv).unwrap();
    assert_eq!(rv, greeting);
}

#[test]
fn test_weird_paths() {
    let tmpdir = get_tmp();
    env::set_current_dir(tmpdir).expect("setup failed");

    AtomicFile::new("foo", AllowOverwrite)
        .write(|f| f.write_all(b"HELLO"))
        .unwrap();
    let mut rv = String::new();
    let mut testfd = fs::File::open("foo").unwrap();
    testfd.read_to_string(&mut rv).unwrap();
    assert_eq!(rv, "HELLO");
}

/// Test the error that is returned if the file already exists
/// with `OverwriteBehavior::DisallowOverwrite`.
#[test]
fn disallow_overwrite_error() -> io::Result<()> {
    let tmp = TempDir::new()?;
    let file = tmp.path().join("dest");
    let af = AtomicFile::new_with_tmpdir(&file, DisallowOverwrite, tmp.path());

    // touch file
    fs::write(&file, "")?;

    match af.write(|f: &mut fs::File| f.write(b"abc")) {
        Ok(_) => panic!("should fail!"),
        Err(e) => {
            let e = io::Error::from(e);
            match e.kind() {
                io::ErrorKind::AlreadyExists => Ok(()),
                _ => Err(e),
            }
        }
    }
}