about summary refs log tree commit diff
path: root/src/actions/create.rs
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-05 19:06:01 +0200
committerMel <einebeere@gmail.com>2021-10-05 19:06:01 +0200
commitfad317349557bee1055d81d7444f99619247815c (patch)
tree50d9e081fcdab32b02daed8d23fa41e8eacd1d83 /src/actions/create.rs
parentad153a44d383962ac8021c02023a92b4c987fd54 (diff)
downloadka-fad317349557bee1055d81d7444f99619247815c.tar.zst
ka-fad317349557bee1055d81d7444f99619247815c.zip
Fs asserts and Create action tests.
Diffstat (limited to 'src/actions/create.rs')
-rw-r--r--src/actions/create.rs116
1 files changed, 113 insertions, 3 deletions
diff --git a/src/actions/create.rs b/src/actions/create.rs
index 56d946b..184e249 100644
--- a/src/actions/create.rs
+++ b/src/actions/create.rs
@@ -3,10 +3,10 @@ use anyhow::Result;
 
 use super::ActionOptions;
 
-pub fn create(command_options: ActionOptions, fs: &impl Fs) -> Result<()> {
+pub fn create(command_options: ActionOptions, fs: &impl Fs, timestamp: u64) -> Result<()> {
     let locations = Locations::from(&command_options);
 
-    if locations.ka_path.exists() {
+    if fs.path_exists(&locations.ka_path) {
         fs.delete_directory(&locations.ka_path)?;
     }
 
@@ -17,7 +17,117 @@ pub fn create(command_options: ActionOptions, fs: &impl Fs) -> Result<()> {
     let empty_history = RepositoryHistory::default();
     empty_history.write_to_file(fs, &mut index_file)?;
 
-    update(command_options, fs)?;
+    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();
+    }
+}