liteboxfs 0.1.0

A modern POSIX filesystem in a SQLite database
Documentation
#![cfg(all(feature = "fs", target_os = "linux"))]

use xpct::{be_ok, equal, expect};

use crate::{
    Connection,
    file_metadata::{FileKind, Owner},
    settings::Settings,
};

fn open() -> crate::Result<Connection> {
    Connection::open_for_testing(&Settings::default())
}

#[test]
fn fresh_filesystem_has_only_root_directory() -> crate::Result<()> {
    let mut conn = open()?;

    conn.exec(|fs| {
        expect!(fs.stats())
            .to(be_ok())
            .map(|stats| stats.file_count())
            .to(equal(1u64));
        crate::Result::Ok(())
    })?;

    Ok(())
}

#[test]
fn file_count_includes_each_created_file() -> crate::Result<()> {
    let mut conn = open()?;

    conn.exec(|fs| {
        fs.create("/a.txt", FileKind::Regular, Owner::ROOT)?;
        fs.create("/b.txt", FileKind::Regular, Owner::ROOT)?;
        fs.create("/c.txt", FileKind::Regular, Owner::ROOT)?;

        expect!(fs.stats())
            .to(be_ok())
            .map(|stats| stats.file_count())
            .to(equal(4u64));
        crate::Result::Ok(())
    })?;

    Ok(())
}

#[test]
fn file_count_includes_directories() -> crate::Result<()> {
    let mut conn = open()?;

    conn.exec(|fs| {
        fs.create("/dir", FileKind::Dir, Owner::ROOT)?;
        fs.create("/dir/sub", FileKind::Dir, Owner::ROOT)?;

        expect!(fs.stats())
            .to(be_ok())
            .map(|stats| stats.file_count())
            .to(equal(3u64));
        crate::Result::Ok(())
    })?;

    Ok(())
}

#[test]
fn file_count_decreases_on_delete() -> crate::Result<()> {
    let mut conn = open()?;

    conn.exec(|fs| {
        fs.create("/a.txt", FileKind::Regular, Owner::ROOT)?;
        fs.create("/b.txt", FileKind::Regular, Owner::ROOT)?;
        fs.delete("/a.txt")?;

        expect!(fs.stats())
            .to(be_ok())
            .map(|stats| stats.file_count())
            .to(equal(2u64));
        crate::Result::Ok(())
    })?;

    Ok(())
}

#[test]
fn hard_links_count_as_one_file() -> crate::Result<()> {
    let mut conn = open()?;

    conn.exec(|fs| {
        let mut file = fs.create("/a.txt", FileKind::Regular, Owner::ROOT)?;
        file.link("/b.txt")?;
        drop(file);

        expect!(fs.stats())
            .to(be_ok())
            .map(|stats| stats.file_count())
            .to(equal(2u64));
        crate::Result::Ok(())
    })?;

    Ok(())
}

#[test]
fn temp_files_do_not_count() -> crate::Result<()> {
    let mut conn = open()?;

    conn.exec(|fs| {
        fs.create_temp(Owner::ROOT)?;

        expect!(fs.stats())
            .to(be_ok())
            .map(|stats| stats.file_count())
            .to(equal(1u64));
        crate::Result::Ok(())
    })?;

    Ok(())
}