Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CLI tests for iroh add, and API changes #343

Merged
merged 4 commits into from
Oct 17, 2022

Conversation

faassen
Copy link
Contributor

@faassen faassen commented Oct 14, 2022

This contains work on the add command, with a few CLI tests and some
API tweaks.

add layering

add_file and add_dir in the core Api that's mockable, and the slightly higher level add is above it in ApiExt.

recursive simplication

Test for the -r flag in the CLI and just let add_dir in the resolver add a directory without worrying. This is tested.

a few add CLI tests

A few basic add CLI tests.

CLI test cleanup

I've went through the cli_tests.rs module step by step cleaning and reorganizing the tests so it's more easy to maintain them. Unfortunately trycmd gets too finicky if I try to use its globbing feature (as input and output directories seem shared sometimes). So now I maintain an explicit list in the same order as they show up alphabetically. It's too verbose but it's the best we can do for now.

@faassen faassen changed the title Cli add feat: CLI tests for iroh add, and API changes Oct 14, 2022
@faassen faassen marked this pull request as ready for review October 14, 2022 17:18
rklaehn
rklaehn previously approved these changes Oct 17, 2022
Copy link
Contributor

@rklaehn rklaehn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Nice tests for the actual command output.

You have to be careful not to get bogged down by these kinds of tests though. We don't want to spend days making sure that every byte coming out of stdout is the same when refactoring.

But the way it is done here seems to be quick to update.

@@ -29,6 +29,16 @@ pub trait ApiExt: Api {
save_get_stream(&root_path, blocks).await?;
Ok(root_path)
}

async fn add(&self, path: &Path, wrap: bool) -> Result<Cid> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the code to figure out if it is a file or a dir is moved to ApiExt, and the main fns in api require you to be explicit? I like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and the check for the recursive flag before copying a directory was removed from the API entirely, because the use case for writing code like this is very different from the reason it's in the CLI (which is to prevent accidents in interactive mode).

store: Option<S>,
path: &Path,
wrap: bool,
recursive: bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the recursive flag not needed anymore? Do we always use recursive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case for the recursive flag in my mind is to prevent people from accidentally adding a directory in the CLI. That use case doesn't exist on the API level, so I've removed it from the API. The implementation is now on the CLI level which is much simpler than maintaining this in the UnixFS builder.

let mut api = MockApi::default();
api.expect_add_file().returning(|_ipfs_path, _| {
Box::pin(future::ready(
Cid::from_str("QmYbcW4tXLXHWw753boCK8Y7uxLu5abXjyYizhLznq9PUR").map_err(|e| e.into()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this returns the same cid whatever you add. Is the intention to make it more realistic, or just write another mock for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just write another mock if that becomes a use case. In general I don't think we should strive for realism in these mocks, just fulfilling the contract in the easiest way possible for a particular test case.

Unfortunately this means for add there isn't all that much to mock yet. Perhaps that will change somewhat once the add API starts to return more stuff related to progress, though I suspect tests for that will have to be normal Rust unit tests, not trycmd tests.

@faassen
Copy link
Contributor Author

faassen commented Oct 17, 2022

You have to be careful not to get bogged down by these kinds of tests though. We don't want to spend days making sure that
every byte coming out of stdout is the same when refactoring.

I agree. trycmd offers features for eliding content and if need be we can use that to be insensitive to bytes we don't care about.

Needs more tests for various scenarios now
It's really only needed there, no need to pass a boolean in deeply. Even the API
doesn't really need this - this is really a flag to help people avoid command-line
mistakes.
@faassen faassen merged commit fdf2170 into n0-computer:main Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants