tempfile 0.2.0

Securely create temporary files.
#![feature(path_ext)]
extern crate tempfile;
use tempfile::NamedTempFile;
use std::env;
use std::io::{Write, Read, Seek, SeekFrom};
use std::fs::{PathExt, File};

#[test]
fn test_basic() {
    let mut tmpfile = NamedTempFile::new().unwrap();
    write!(tmpfile, "abcde").unwrap();
    tmpfile.seek(SeekFrom::Start(0)).unwrap();
    let mut buf = String::new();
    tmpfile.read_to_string(&mut buf).unwrap();
    assert_eq!("abcde", buf);
}

#[test]
fn test_deleted() {
    let tmpfile = NamedTempFile::new().unwrap();
    let path = tmpfile.path().to_path_buf();
    assert!(path.exists());
    drop(tmpfile);
    assert!(!path.exists());
}

#[test]
fn test_into_path() {
    let tmpfile = NamedTempFile::new().unwrap();
    assert!(tmpfile.path().exists());
    let pathbuf = tmpfile.into_path();
    assert!(pathbuf.exists());
    std::fs::remove_file(pathbuf).unwrap();
}

#[test]
fn test_persist() {
    let mut tmpfile = NamedTempFile::new().unwrap();
    let old_path = tmpfile.path().to_path_buf();
    let persist_path = env::temp_dir().join("persisted_temporary_file");
    write!(tmpfile, "abcde").unwrap();
    {
        assert!(old_path.exists());
        let mut f = tmpfile.persist(&persist_path).unwrap();
        assert!(!old_path.exists());

        // Check original file
        f.seek(SeekFrom::Start(0)).unwrap();
        let mut buf = String::new();
        f.read_to_string(&mut buf).unwrap();
        assert_eq!("abcde", buf);
    }

    {
        // Try opening it at the new path.
        let mut f = File::open(&persist_path).unwrap();
        f.seek(SeekFrom::Start(0)).unwrap();
        let mut buf = String::new();
        f.read_to_string(&mut buf).unwrap();
        assert_eq!("abcde", buf);
    }
    std::fs::remove_file(&persist_path).unwrap();
}