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

Use terraform-exec #268

Closed
wants to merge 4 commits into from
Closed

Use terraform-exec #268

wants to merge 4 commits into from

Conversation

appilon
Copy link
Contributor

@appilon appilon commented Sep 17, 2020

Will bump terraform-exec to proper release before merging, but ready for review. Previously we maintained an "executor" library internally, the implementation had a nice trick that allowed it to become mocked for unit testing. While switching to terraform-exec was relatively easy, decoupling the built in mock required some refactoring.

This represents the least obtrusive drop in replacement of terraform-exec I could think of. With further refactoring we could likely remove the wrapping Executor struct. This refactor tried to strictly follow placing the responsibility of interface definitions on the consuming packages, which allows the producing packages to work in concrete types, and thus not forcing 1 large monolithic interface just for the sake of a mock to build up, albeit the interface created in rootmodule covers most of the surface area.

Closes #266

Move exec tests to exec_mock_test.go since they don't use the concrete
implementation.
@appilon appilon marked this pull request as ready for review September 23, 2020 01:41
}
tf.SetLogger(e.logger)

// TODO: make sense of how this works
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still need to reason about how this worked

internal/terraform/exec/exec.go Outdated Show resolved Hide resolved
version := strings.TrimPrefix(lines[0], "Terraform v")

return version, nil
// TODO: consider refactoring codebase to work directly with go-version.Version
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed a fair bit of back and forth between go-version.Version and the stringified representation. I think given I saw go-version types bled onto a few structs throughout the codebade, and given terraform-exec works with it directly it may be worth changing the language server codebase to work directly in that type, in a follow up PR.

return formatterForVersion(v, m.Format)
}

func (m *mockExecutor) run(ctx context.Context, args ...string) ([]byte, error) {
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 mockExecutor is essentially a stripped down copy of the previous "melded" implementation we had before

@@ -13,3 +17,82 @@ func TestMain(m *testing.M) {

os.Exit(m.Run())
}

func TestExec_timeout(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests were moved out of exec_test.go because I felt it was just testing the mock.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah i think you can just drop them

@@ -210,65 +112,30 @@ func (e *Executor) FormatterForVersion(v string) (Formatter, error) {
}

if ver.GreaterThanOrEqual(fmtCapableVersion) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could move this version compatibility check to terraform-exec if you wanted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is something @radeksimko upstreaming now. Depending on the outcome of the upstreaming work, this PR may be superseded.

Copy link
Member

Choose a reason for hiding this comment

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

return &schemas, nil
func contextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
cancel := func() {}
if timeout > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

should it just be a WithCancel if its zero?

@radeksimko
Copy link
Member

Just to summarize what we discussed via Zoom today:

  1. templating support in logging paths - We could upstream that by introducing a method similar to SetLogPath (e.g. SetLogPathTemplate). I will look into that.
  2. Formatting method with []byte-compatible API - This is pending some questions about what the right name and API is. io.Writer+io.Reader for stdin/stdout may be better, but I'm unsure how well that fits next to FormatWrite and also whether we should sunset FormatString, or if we just introduce FormatBytes. Pinging @kmoe for opinions here.
  3. Mocking library - I will look into upstreaming it.
  4. Version handling in formatter - fmt: Gate formatting command on 0.7.7 terraform-exec#88

@ghost
Copy link

ghost commented Nov 7, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the context necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 7, 2020
@radeksimko radeksimko deleted the terraform-exec branch November 19, 2020 11:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use terraform-exec
3 participants