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

test: Refactor modules to use mocked commands #936

Merged
merged 2 commits into from Feb 21, 2020

Conversation

andytom
Copy link
Member

@andytom andytom commented Feb 13, 2020

Description

Have refactored the ruby and php modules tests to use a mocked
command and no longer depend on having a particular version of
Ruby or PHP installed. While I was here I also added a quick test for
checking the formatting of the ruby output.

Motivation and Context

This will make testing easier and less fragile.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Screenshots (if appropriate):

How Has This Been Tested?

  • I have tested using MacOS
  • I have tested using Linux
  • I have tested using Windows

Checklist:

  • I have updated the documentation accordingly.
  • I have updated the tests accordingly.

@andytom andytom changed the title tests: Refactor modules to use mocked commands test: Refactor modules to use mocked commands Feb 13, 2020
Have refactored the ruby module tests to use a mocked command and no
longer depend on having a particular version of Ruby installed. While I
was here I also added a quick test for checking the formatting of the
ruby output.
Have refactored the php module tests to use a mocked command and no
longer depend on having a particular version of PHP installed.
@andytom andytom requested a review from a team February 14, 2020 21:15
@andytom andytom force-pushed the refactor/move_tests branch 2 times, most recently from 2f4a79d to efa222c Compare February 14, 2020 21:28
@andytom andytom marked this pull request as ready for review February 14, 2020 21:36
@andytom
Copy link
Member Author

andytom commented Feb 14, 2020

I have been looking at migrating some of the other modules (python, terraform, etc), but it looks like they depend on having some env vars set. I've done a bit off Googling and I'm not sure what the best way to handle them in the test render is, does anyone have any suggestions?

@matchai
Copy link
Member

matchai commented Feb 15, 2020

I'm afraid it's not going to be easy. We may have create an abstraction around std::env::var, and a setter for tests.

For example:

#[test]
fn check_env_var() -> io::Result<()> {
  // starting now, `getEnvVar("USER")` will return `Some("root")`
  mockEnvVar("USER", "root");

  let actual = render_module("username", dir.path());
}

In fact, this may also be the preferable pattern for mocking command execution, as it lets us keep the expected mock context near the test code.

What are your thoughts, @andytom?

Copy link
Member

@matchai matchai 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! 👍
Just a quick comment below, and it should be ready to merge. 😄

src/utils.rs Outdated Show resolved Hide resolved
@andytom
Copy link
Member Author

andytom commented Feb 15, 2020

In fact, this may also be the preferable pattern for mocking command execution, as it lets us keep the expected mock context near the test code.

What are your thoughts, @andytom?

I was thinking something similar, while it made the initial implementation easy the current way of doing things does have it's limitations. In addition to keeping the tests and the mocks closer together this would allow us to set different responses for on a per test basis, this would be useful for Java and Python where some versions print the version strings on stdout and others use stderr. Also the case statement is getting a bit unwieldy and we haven't even migrated all the other commands in.

@andytom andytom requested a review from matchai February 20, 2020 21:42
@matchai matchai merged commit d44c037 into starship:master Feb 21, 2020
@andytom andytom deleted the refactor/move_tests branch February 22, 2020 10:58
dagbrown pushed a commit to dagbrown/starship that referenced this pull request Oct 22, 2021
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.

None yet

2 participants