Repository: sharkdp/fd
File: tests/tests.rs (Rust)

Code before:
    let files = &[];
    let te = TestEnv::new(dirs, files);

    fs::File::create(
        te.test_root()
            .join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),
    ).unwrap();

    te.assert_output(&["", "test1/"], "test1/test_�invalid.txt");

    te.assert_output(&["invalid", "test1/"], "test1/test_�invalid.txt");

    // Should not be found under a different extension
    te.assert_output(&["-e", "zip", "", "test1/"], "");
}

/// (--size)
#[test]
fn test_size() {
    let dirs = &["test1", "test2"];
    let files = &["test1/a.foo", "test1/b.foo", "test1/c.foo"];
    let te = TestEnv::new(dirs, files);

    let mut f = fs::File::create(te.test_root().join("test1/tiny.foo")).unwrap();
    f.write("Hello world".as_bytes()).unwrap(); // 11 bytes
    f.flush().unwrap();

    let mut f = fs::File::create(te.test_root().join("test2/big.foo")).unwrap();
    f.write("Hello user, fd is a nice tool.".as_bytes()).unwrap(); // 30 bytes
    f.flush().unwrap();

    // Zero and non-zero sized files.
    te.assert_output(
        &["", "test1", "test2", "--size", "+0B"],
        "test1/a.foo
        test1/b.foo
        test1/c.foo
        test1/tiny.foo
        test2/big.foo"
    );

    // Zero sized files.
    te.assert_output(
        &["", "test1", "test2", "--size", "-0B"],
        "test1/a.foo
        test1/b.foo
        test1/c.foo"
    );

    // Files with 2 bytes or more.
    te.assert_output(

Review comment (refactor):
Maybe it'd be worth to put these lines into a little helper function:

``` rust
fn create_file_with_size(filename: &str, size_in_bytes: usize) {
    let content = "#".repeat(size_in_bytes);
    let mut f = File::create(filename).unwrap();
    f.write(content.as_bytes()).unwrap();
}
```

The `flush()` call would not be necessary because the files would be closed properly at the end of that helper function when `f` goes out of scope.

Also, I think the tests would be a little bit easier to read if the files would be called `11_byte.foo` and `30_byte.foo`.

Code after:
/// Filenames with invalid UTF-8 sequences
#[cfg(target_os = "linux")]
#[test]
fn test_invalid_utf8() {
    use std::ffi::OsStr;
    use std::os::unix::ffi::OsStrExt;

    let dirs = &["test1"];
    let files = &[];
    let te = TestEnv::new(dirs, files);

    fs::File::create(
        te.test_root()
            .join(OsStr::from_bytes(b"test1/test_\xFEinvalid.txt")),
    ).unwrap();

    te.assert_output(&["", "test1/"], "test1/test_�invalid.txt");

    te.assert_output(&["invalid", "test1/"], "test1/test_�invalid.txt");

    // Should not be found under a different extension
    te.assert_output(&["-e", "zip", "", "test1/"], "");
}

/// (--size)
#[test]
fn test_size() {
    let dirs = &["test1", "test2"];
    let files = &["test1/a.foo", "test1/b.foo", "test1/c.foo"];
    let te = TestEnv::new(dirs, files);

    create_file_with_size(te.test_root().join("test1/11_bytes.foo"), 11);
    create_file_with_size(te.test_root().join("test2/30_bytes.foo"), 30);
    create_file_with_size(te.test_root().join("test1/3_kilobytes.foo"), 3 * 1000);
    create_file_with_size(te.test_root().join("test2/4_kibibytes.foo"), 4 * 1024);

    // Zero and non-zero sized files.
    te.assert_output(
        &["", "test1", "test2", "--size", "+0B"],
        "test1/11_bytes.foo
        test1/a.foo
        test1/b.foo
        test1/c.foo
        test1/3_kilobytes.foo
        test2/30_bytes.foo
        test2/4_kibibytes.foo"
    );

    // Zero sized files.
    te.assert_output(
        &["", "test1", "test2", "--size", "-0B"],