Skip to content

Commit

Permalink
Merge pull request #16 from dob9601/improve-coverage
Browse files Browse the repository at this point in the history
Add --naive flag, tests for manifest sync
  • Loading branch information
dob9601 authored Apr 18, 2022
2 parents 0038034 + 6b0a3af commit 78d078a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SUBCOMMANDS:

jointhedots works by reading a "jtd.yaml" manifest file located within your dotfile repository. The manifest contains a mapping of file to installed location (amongst other things), allowing for JTD to automatically install configurations. `pre_install` and `post_install` commands can also be specified, allowing for additional control over installation.

jtd also allows for pushing your dotfiles back to the remote repo and resolves merge conflicts via git.
jtd also allows for pushing your dotfiles back to the remote repo and resolves merges via git. It's also possible to avoid all prompts for input. This, combined with the fact that jtd is deterministic, makes it very suitable for for use in scripts.

These install steps are designed so that they will run once on your first install, store a hash of the steps run and then only run if the hash differs (i.e. you have modified your config with new install steps).

Expand All @@ -56,8 +56,8 @@ These install steps are designed so that they will run once on your first instal
| Support for non-GitHub/GitLab repos | | |
| Ability to manually specify commit message for JTD sync || |
| More detailed default commit messages for JTD sync (list the changed files) || |
| Abort syncing if no changes are present in files | | |
| Don't allow `jtd install` if dotfiles are behind remote main (prompt user to sync) | | |
| Abort syncing if no changes are present in files | | |
| Don't allow `jtd install` if dotfiles are behind remote main (prompt user to sync) | | |

## Installation

Expand Down
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ pub struct SyncSubcommandArgs {
short = 'c'
)]
pub commit_msg: Option<String>,

#[clap(
help = "Whether to use naive sync. If not present, git-based sync will be used unless metadata \
is unavailable in which case you will be prompted as to whether you wish to fallback to naive sync.",
long = "naive"
)]
pub naive: bool,
}

#[derive(clap::Args, Debug)]
Expand Down
42 changes: 14 additions & 28 deletions src/structs/dotfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,11 @@ mod tests {
let repo = Repository::init(&repo_dir).expect("Could not initialise repository");

// Create file in repo
let mut repo_filepath = repo_dir.path().to_owned();
repo_filepath.push(Path::new("dotfile"));
let repo_filepath = repo_dir.path().to_owned().join("dotfile");
File::create(repo_filepath.to_owned()).expect("Could not create file in repo");

// Create dotfile "on the local system"
let mut local_filepath = dotfile_dir.path().to_owned();
local_filepath.push(Path::new("dotfile"));
let local_filepath = dotfile_dir.path().to_owned().join("dotfile");
File::create(local_filepath.to_owned()).expect("Could not create file in tempdir");

let commit = add_and_commit(
Expand Down Expand Up @@ -502,13 +500,11 @@ mod tests {
let repo = Repository::init(&repo_dir).expect("Could not initialise repository");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");

// Create dotfile "on the local system" with different contents
let mut filepath = dotfile_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = dotfile_dir.path().to_owned().join("dotfile");
let mut dotfile_file =
File::create(filepath.to_owned()).expect("Could not create file in tempdir");
dotfile_file
Expand Down Expand Up @@ -549,8 +545,7 @@ mod tests {
let target_path = dotfile_dir.path().join("dotfile");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");

let _commit = add_and_commit(
Expand Down Expand Up @@ -588,8 +583,7 @@ mod tests {
let target_touch_post_install = dotfile_dir.path().join("post_install");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");

let _commit = add_and_commit(
Expand Down Expand Up @@ -632,13 +626,11 @@ mod tests {
let target_path = dotfile_dir.path().join("dotfile");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");

// Create dotfile "on the local system"
let mut local_filepath = dotfile_dir.path().to_owned();
local_filepath.push(Path::new("dotfile"));
let local_filepath = dotfile_dir.path().to_owned().join("dotfile");
let mut file =
File::create(local_filepath.to_owned()).expect("Could not create file in tempdir");
file.write_all(b"These are local changes on the system")
Expand Down Expand Up @@ -678,8 +670,7 @@ mod tests {
let target_path = dotfile_dir.path().join("dotfile");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");
let _commit = add_and_commit(
&repo,
Expand All @@ -691,8 +682,7 @@ mod tests {
.expect("Failed to commit to repository");

// Create dotfile "on the local system"
let mut local_filepath = dotfile_dir.path().to_owned();
local_filepath.push(Path::new("dotfile"));
let local_filepath = dotfile_dir.path().to_owned().join("dotfile");
let mut file =
File::create(local_filepath.to_owned()).expect("Could not create file in tempdir");
file.write_all(b"These are local changes on the system")
Expand Down Expand Up @@ -725,8 +715,7 @@ mod tests {
let target_path = dotfile_dir.path().join("dotfile");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");
let _commit = add_and_commit(
&repo,
Expand All @@ -738,8 +727,7 @@ mod tests {
.expect("Failed to commit to repository");

// Create dotfile "on the local system"
let mut local_filepath = dotfile_dir.path().to_owned();
local_filepath.push(Path::new("dotfile"));
let local_filepath = dotfile_dir.path().to_owned().join("dotfile");
let mut file =
File::create(local_filepath.to_owned()).expect("Could not create file in tempdir");
file.write_all(b"These are local changes on the system")
Expand Down Expand Up @@ -778,8 +766,7 @@ mod tests {
let target_path = dotfile_dir.path().join("dotfile");

// Create file in repo
let mut filepath = repo_dir.path().to_owned();
filepath.push(Path::new("dotfile"));
let filepath = repo_dir.path().to_owned().join("dotfile");
File::create(filepath.to_owned()).expect("Could not create file in repo");
let _commit = add_and_commit(
&repo,
Expand All @@ -791,8 +778,7 @@ mod tests {
.expect("Failed to commit to repository");

// Create dotfile "on the local system"
let mut local_filepath = dotfile_dir.path().to_owned();
local_filepath.push(Path::new("dotfile"));
let local_filepath = dotfile_dir.path().to_owned().join("dotfile");
File::create(local_filepath.to_owned()).expect("Could not create file in tempdir");

let dotfile = Dotfile {
Expand Down
52 changes: 50 additions & 2 deletions src/structs/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,14 @@ impl Manifest {
target_dotfiles: Vec<String>,
commit_msg: Option<&str>,
aggregated_metadata: Option<AggregatedDotfileMetadata>,
use_naive_sync: bool,
) -> Result<(), Box<dyn Error>> {
let theme = get_theme();

let dotfiles = self.get_target_dotfiles(target_dotfiles, sync_all);
let mut commit_hashes = vec![];

if aggregated_metadata.is_none() {
if aggregated_metadata.is_none() && !use_naive_sync {
println!(
"{}",
style(
Expand Down Expand Up @@ -280,7 +281,7 @@ impl IntoIterator for Manifest {
#[cfg(test)]
mod tests {
use std::{
fs::File,
fs::{read_to_string, File},
io::Write,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -344,4 +345,51 @@ kitty:
.expect("Failed to install manifest");
assert!(Path::exists(&target_path));
}

#[test]
fn test_manifest_sync() {
let repo_dir = tempdir().expect("Could not create temporary repo dir");
let repo = Repository::init(&repo_dir).expect("Could not initialise repository");

let dotfile_dir = tempdir().expect("Could not create temporary dotfile dir");
let target_path = dotfile_dir.path().join("dotfile");

// Create file in repo
let repo_dotfile_path = repo_dir.path().join("dotfile");
File::create(repo_dotfile_path.to_owned()).expect("Could not create file in repo");
let _commit = add_and_commit(
&repo,
Some(vec![&repo_dotfile_path]),
"commit message",
Some(vec![]),
Some("HEAD"),
)
.expect("Failed to commit to repository");

// Create dotfile "on the local system"
let mut file =
File::create(target_path.to_owned()).expect("Could not create file in tempdir");
file.write_all(b"These are local changes on the system")
.expect("Failed to write to dotfile");

let manifest: Manifest = serde_yaml::from_str(
&SAMPLE_MANIFEST.replace("~/some/path/here", &target_path.to_string_lossy()),
)
.unwrap();

let err = manifest
.sync(&repo, true, vec![], None, None, true)
.unwrap_err();

// FIXME: This is a very dodgy test, maybe setup a mock repo for pushing to?
assert_eq!(
err.to_string(),
"remote 'origin' does not exist; class=Config (7); code=NotFound (-3)"
);

assert_eq!(
read_to_string(&repo_dotfile_path).unwrap(),
"These are local changes on the system"
);
}
}
1 change: 1 addition & 0 deletions src/subcommands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ pub fn sync_subcommand_handler(args: SyncSubcommandArgs) -> Result<(), Box<dyn E
args.target_dotfiles,
args.commit_msg.as_deref(),
AggregatedDotfileMetadata::get()?,
args.naive,
)
}

0 comments on commit 78d078a

Please sign in to comment.