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

add "remove" command #60

Merged
merged 1 commit into from
Jan 31, 2024
Merged

add "remove" command #60

merged 1 commit into from
Jan 31, 2024

Conversation

marco-m-pix4d
Copy link
Contributor

Part of PCI-3617

@marco-m-pix4d
Copy link
Contributor Author

I can say that it would have been simply impossible to address the PR in repo-settings without this feature of terravalet!
Hundreds of resources.

os.Exit(Main())
}

func Main() int {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why there is 2 main / Main functions?

Copy link
Contributor Author

@marco-m-pix4d marco-m-pix4d Jan 31, 2024

Choose a reason for hiding this comment

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

@iAmoric

To understand, it helps to keep in mind that Go is strongly typed. This makes it easy to follow the flow.

A Go program wants a main function with name and signature main(). That is, it takes nothing and returns nothing. (This is not the same as None in Python or nil in Go, it is really nothing).

Then we have Main() int, which returns an integer. This could have been called anything. We call it Main as convention, because it is doing everything the main() is doing.

Why we added it? Because of

     1  func TestMain(m *testing.M) {
     2      // The commands map holds the set of command names, each with an associated
     3      // run function which should return the code to pass to os.Exit.
     4      // When [testscript.Run] is called, these commands are installed as regular
     5      // commands in the shell path, so can be invoked with "exec".
     6      os.Exit(testscript.RunMain(m, map[string]func() int{
     7          "terravalet": Main,
     8      }))
     9  }

See line 7 ? it calls this Main() and, see line 6, it is using its return code for os.Exit.

The power of testscript, introduced in this PR, is that when it sees line 7, it also compiles the test executable. Said in another way, it allows to drive integration tests directly from Go code with the standard Go tooling (go test), ensuring that dependencies are built before the tests are run.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you. So if I summarize roughly, you need Main(), which returns an integer, because you need a return value when using TestMain but main() returns nothing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iAmoric exactly.

Then we could ask ourselves why main() returns nothing. There are reasons, rooted in the fact that not all OSes are Unix. But this is another story :-)

@marco-m-pix4d marco-m-pix4d merged commit 6eb197d into master Jan 31, 2024
2 checks passed
@marco-m-pix4d marco-m-pix4d deleted the cmd-remove branch January 31, 2024 08:44
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.

4 participants