about summary refs log tree commit diff
path: root/src/actions/create.rs
blob: 184e24964745785ab3b52f4c7d7b6172054d4be8 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::{actions::update, files::Locations, filesystem::Fs, history::RepositoryHistory};
use anyhow::Result;

use super::ActionOptions;

pub fn create(command_options: ActionOptions, fs: &impl Fs, timestamp: u64) -> Result<()> {
    let locations = Locations::from(&command_options);

    if fs.path_exists(&locations.ka_path) {
        fs.delete_directory(&locations.ka_path)?;
    }

    fs.create_directory(&locations.ka_path)?;
    fs.create_directory(&locations.ka_files_path)?;

    let mut index_file = fs.create_file(&locations.get_repository_index_path())?;
    let empty_history = RepositoryHistory::default();
    empty_history.write_to_file(fs, &mut index_file)?;

    update(command_options, fs, timestamp)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::{path::Path, vec};

    use crate::{
        actions::{create, ActionOptions},
        diff::ContentChange,
        filesystem::mock::{EntryMock, ExpectedCall as Call, ExpectedCallVariant as Type, FsMock},
        history::{
            FileChange, FileChangeVariant, FileHistory, RepositoryChange, RepositoryHistory,
        },
    };

    #[test]
    fn create_empty() {
        let now = 0xC0FFEE;
        let fs_mock = FsMock::new();
        let options = ActionOptions::from_path(".");

        let pwd = Path::new(".");
        let working_file = Path::new("./test");
        let history_file = Path::new("./.ka/files/test");

        let ka = Path::new("./.ka");
        let ka_files = Path::new("./.ka/files");
        let ka_index = Path::new("./.ka/index");

        let empty_index = RepositoryHistory::default().encode().unwrap();
        let expected_index = {
            let mut history = RepositoryHistory::default();
            history.add_change(RepositoryChange {
                affected_files: vec![working_file.to_path_buf()],
                timestamp: now,
            });
            history.cursor = 1;
            history.encode().unwrap()
        };

        let expected_file_history = {
            let mut history = FileHistory::default();
            let change = ContentChange::Inserted {
                at: 0,
                new_content: vec![1, 2, 3],
            };
            history.add_change(FileChange {
                change_index: 1,
                variant: FileChangeVariant::Updated(vec![change]),
            });
            history.encode().unwrap()
        };

        fs_mock.set_expected_calls(vec![
            // Create calls
            Call::new(ka, Type::PathExists(false)),
            Call::new(ka, Type::CreateDirectory),
            Call::new(ka_files, Type::CreateDirectory),
            Call::new(ka_index, Type::CreateFile),
            Call::new(
                ka_index,
                Type::WriteToFile {
                    expected: empty_index.clone(),
                },
            ),
            // Update calls
            Call::new(ka_index, Type::OpenWritableFile),
            Call::new(
                ka_index,
                Type::ReadFile {
                    returned: empty_index,
                },
            ),
            Call::new(
                pwd,
                Type::ReadDirectory {
                    returned: vec![
                        EntryMock::new(working_file, false),
                        EntryMock::new(ka, true),
                    ],
                },
            ),
            Call::new(ka_files, Type::ReadDirectory { returned: vec![] }),
            Call::new(history_file, Type::PathExists(false)),
            Call::new(working_file, Type::OpenReadableFile),
            Call::new(
                working_file,
                Type::ReadFile {
                    returned: vec![1, 2, 3],
                },
            ),
            Call::new(history_file, Type::CreateFile),
            Call::new(
                history_file,
                Type::WriteToFile {
                    expected: expected_file_history,
                },
            ),
            Call::new(
                ka_index,
                Type::WriteToFile {
                    expected: expected_index,
                },
            ),
        ]);

        create(options, &fs_mock, now).expect("Action failed.");

        fs_mock.assert_calls();
    }
}