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: add CLI tests #216

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

feat: add CLI tests #216

wants to merge 5 commits into from

Conversation

willemneal
Copy link
Member

@willemneal willemneal commented Feb 17, 2023

Currently the docs have example cli commands for the examples and this provides tests to ensure that the docs are accurate.

@gitpod-io
Copy link

gitpod-io bot commented Feb 17, 2023


const WASM: &Wasm = &Wasm::Release("soroban_auth_contract");

const ACC1: &str = "GA6S566FD3EQDUNQ4IGSLXKW3TGVSTQW3TPHPGS7NWMCEIPBOKTNCSRU";
Copy link
Member Author

Choose a reason for hiding this comment

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

Please write out. No need to be so terse in an example test.

Copy link
Contributor

Choose a reason for hiding this comment

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

done in d6bf4f8

@willemneal willemneal marked this pull request as ready for review February 24, 2023 15:33

[dev-dependencies]
predicates = "2.1.5"
soroban-test = { git = "https://github.com/ahalabs/soroban-tools", rev = "bc1d008" }
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be the stellar repo, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, relevant PR is here:

We can wait to merge these examples tests until the soroban-tools PR is merged.

@paulbellamy
Copy link
Contributor

The tests themselves look fine. But they feel like cli tests, so it's odd they're in the examples repo. Feels like they should be in the soroban-tools repo.

@tomerweller
Copy link
Contributor

I agree with @paulbellamy that these tests don't naturally fit in the examples repo. I think that in a perfect world they would be in the docs repo, as the docs are what is actually being tested. However, the docs don't currently have machinery to facilitate that. So for now having them here sounds like a reasonable compromise to me.

I suggest the following:

  1. rename cli-tests to docs-tests or docs-cli-tests to reflect the fact that we're testing docs, not the cli
  2. add a short README to this new tests package to explain what it does
  3. add a rustdoc at the top of every test file that links to the relevant doc it's testing

Thoughts @willemneal @chadoh?

@willemneal
Copy link
Member Author

I think doc-tests is a good name for now. Ideally we would make the examples a git submodule and then the docs repo could host the tests there.

@tomerweller
Copy link
Contributor

I do wonder if there's a more ergonomic way to write these tests. Constructing these commands in code is somewhat painful and will be painful to update.

Is there a way to use the cli's parser to parse a single command string instead of constructing it by code? I think this will make writing and updating these tests easier - just copy&paste the cli command.

@chadoh
Copy link
Contributor

chadoh commented Feb 24, 2023

@tomerweller Willem and I would rather have the ability to move somewhat in the other direction.

^ this would allow us to:

  • depend on a specific version of the CLI
  • write Rust tests that execute Rust code, rather than calling out to an external CLI process
  • speed up the tests

What do you think?

@chadoh
Copy link
Contributor

chadoh commented Feb 24, 2023

I suggest the following:

  • rename cli-tests to docs-tests or docs-cli-tests to reflect the fact that we're testing docs, not the cli
  • add a short README to this new tests package to explain what it does
  • add a rustdoc at the top of every test file that links to the relevant doc it's testing

All done.

Looks like someone needs to approve running the workflow so tests actually run.

@tomerweller
Copy link
Contributor

tomerweller commented Feb 24, 2023

write Rust tests that execute Rust code, rather than calling out to an external CLI process

I totally agree about writing tests in rust and not calling an external CLI process.

My question/wonder was whether there's a way in code to construct the cli command, using the library, instead of "manually" constructing it.

So instead of this

TestEnv::with_default(|e| {
e.new_cmd("contract")
  .arg("invoke")
  .arg("--wasm")
  .arg(&WASM.path())
  .args(["--id", "1"])
  .args(["--fn", "increment"])
  .args(["--"])
  .args(["--incr", "5"])
  .assert()
  .stderr("")
  .stdout("5\n");
});

One could potentially write something more "similar" to this:

TestEnv::with_default(|e| {
  e.new_cmd_pasre("contract invoke --wasm {} --id 1 --fn increment -- --incr 5", &WASM.path())
    .assert()
    .stderr("")
    .stdout("5\n");
});

This makes an assumption that there's an available parse function (or that one could be exposed) in the cli lib. Intuitively I think there should be one because the soroban-cli indeed parses commands from the cli that take that shape (but my intuition is often wrong). What's the feasibility of this?

@willemneal
Copy link
Member Author

The idea would be to allow constructing an invoke, or any command, and then call a run function that returns the output.

We have tests in the cli to handle testing cli parsing but we can skip that part and have proper types.

@chadoh chadoh force-pushed the feat/tests branch 2 times, most recently from ad807ac to 3d5998e Compare February 27, 2023 21:08
version = "0.0.0"
authors = ["Stellar Development Foundation <info@stellar.org>"]
license = "Apache-2.0"
edition = "2021"

Choose a reason for hiding this comment

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

not an expert, but shouldn't this be 2023 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a 2023 edition (yet)? I don't see it listed in the edition guide.


[dev-dependencies]
predicates = "2.1.5"
soroban-test = { git = "https://github.com/ahalabs/soroban-tools", rev = "698479c" }

Choose a reason for hiding this comment

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

shouldn't this be pointed to an official release ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, see related discussion from last week

Copy link
Contributor

Choose a reason for hiding this comment

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

This now points at an official release! Please mark this comment resolved.

@chadoh
Copy link
Contributor

chadoh commented Apr 5, 2023

@tomerweller @tsachiherman @paulbellamy this now uses the v0.7.1 versions of both soroban-cli and soroban-test. I'd love another round of review.

soroban-test's "new way" of doing this is a little frustrating still, because soroban-cli does not allow running commands in a consistent way. @willemneal's thought on how to fix this is to make a generic async run function that all commands implement, so that the tests can be written in a consistent asynchronous way. This would also allow us to make the tests run against an RPC node down the line, potentially running the exact same tests and only setting an environment variable.

However, even without this, we believe that these tests are a great start. I think it's time to merge this PR; we can open new ones to update these tests as the testing library and CLI evolve.

Copy link
Contributor

@paulbellamy paulbellamy left a comment

Choose a reason for hiding this comment

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

LGTM. It would be nice to have these run as part of CI

@chadoh chadoh force-pushed the feat/tests branch 3 times, most recently from 9a3b573 to e3f9532 Compare April 6, 2023 19:15
This makes use of the newly-published
[soroban-test](https://docs.rs/soroban-test/latest/soroban_test/) crate
and the newly-reorganized [soroban-cli](https://docs.rs/soroban-cli)
library to test all commands shown in the docs.

This gives us confidence that all of these commands work, though it does
come with some downsides:

- these tests live far from the thing they are meant to test
  - could we generate the docs from this repository, rather than keeping
    them in sync with each other?
  - maybe, but it's a little unclear how that would work
  - this is a good starting point

Another shortcoming of the approach here: it still makes use of
`assert_cmd` in some tests, which runs the version of `soroban-cli`
installed on the host system, making it quite brittle. This is necessary
because some commands (`Cmd`s) from `soroban-cli` do not currently
operate in a test-friendly way. This can be seen in a secondary way,
too: not all `Cmd`s can be run the same way. Some use `run`, some use
`run_in_sandbox`, some take arguments, some do not. These concerns can
all be addressed together by refactoring `soroban-cli` to make all
`Cmd`s implement an asynchronous `run` method with the same signature,
and which will return a struct containing both `stdout` and `stderr`.

However, even with these shortcomings and downsides, it is still worth
merging these tests as-is, since it will give us higher confidence that
everything works as expected with the current code. We can refactor
these tests later, and potentially move them to a different repo.

Co-authored-by: Chad Ostrowski <221614+chadoh@users.noreply.github.com>
@chadoh
Copy link
Contributor

chadoh commented Apr 11, 2023

@paulbellamy can you approve the workflow to run?

@paulbellamy
Copy link
Contributor

Nope, I don't have write access to this repo. Maybe @leighmcculloch can?

@tsachiherman
Copy link

Just did. Could you address the conflicts ?

* main:
  Add a very simple account contract example. (stellar#227)
@paulbellamy
Copy link
Contributor

paulbellamy commented Apr 21, 2023

@chadoh Seems like the test runner is failing to launch the soroban cli. Is it actually being built where expected?

Edit: Seems like the .github/workflows/rust.yml probably needs to cargo install --locked soroban-cli before running the tests. Good luck getting it to install the version matching whats in test/doc-tests tho 🤷

@paulbellamy
Copy link
Contributor

paulbellamy commented Apr 24, 2023

Aaaaaaand the top-level Cargo.toml was removed, so there's no more make build-test-wasms, and this is broken now. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants