diff options
| author | Mel <einebeere@gmail.com> | 2021-10-09 20:30:05 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2021-10-09 20:30:05 +0200 |
| commit | e619e85ae4ce3765030728eb3190ce7605b58d43 (patch) | |
| tree | f062b20b25fdc0f745dccb7a339c1e0c4aac11c7 /src/actions | |
| parent | bf87336cbd5225ba9468d840de0c61f764cc23c4 (diff) | |
| download | ka-e619e85ae4ce3765030728eb3190ce7605b58d43.tar.zst ka-e619e85ae4ce3765030728eb3190ce7605b58d43.zip | |
Add tests for Create action.
Diffstat (limited to 'src/actions')
| -rw-r--r-- | src/actions/create.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/actions/create.rs b/src/actions/create.rs index bc7f7b0..2b664b3 100644 --- a/src/actions/create.rs +++ b/src/actions/create.rs @@ -21,3 +21,74 @@ pub fn create(command_options: ActionOptions, fs: &impl Fs, timestamp: u64) -> R Ok(()) } + +#[cfg(test)] +mod tests { + use std::path::Path; + + use crate::{actions::ActionOptions, diff::ContentChange, filesystem::mock::{EntryMock, FsMock, FsState}, history::{FileChange, FileChangeVariant, FileHistory, RepositoryChange, RepositoryHistory}}; + + use super::create; + + #[test] + fn create_empty() { + let now = 0xC0FFEE; + let fs_mock = FsMock::new(); + let options = ActionOptions::from_path("."); + + let expected_index = RepositoryHistory::default().encode().unwrap(); + + create(options, &fs_mock, now).expect("Action failed."); + + fs_mock.assert_match(FsState::new(vec![ + EntryMock::dir("./.ka"), + EntryMock::file("./.ka/index", &expected_index), + EntryMock::dir("./.ka/files"), + ])); + } + + #[test] + fn create_basic() { + let now = 0xC0FFEE; + let mut fs_mock = FsMock::new(); + let options = ActionOptions::from_path("."); + + let expected_index = { + let mut history = RepositoryHistory::default(); + history.add_change(RepositoryChange { + affected_files: vec![Path::new("./test").into()], + 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_state(FsState::new(vec![ + EntryMock::file("./test", &vec![1, 2, 3]) + ])); + + create(options, &fs_mock, now).expect("Action failed."); + + fs_mock.assert_match(FsState::new(vec![ + EntryMock::file("./test", &vec![1, 2, 3]), + + EntryMock::dir("./.ka"), + EntryMock::file("./.ka/index", &expected_index), + EntryMock::dir("./.ka/files"), + EntryMock::file("./.ka/files/test", &expected_file_history), + ])) + } +} \ No newline at end of file |
