-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
Part of PCI-3617
I can say that it would have been simply impossible to address the PR in repo-settings without this feature of terravalet! |
os.Exit(Main()) | ||
} | ||
|
||
func Main() int { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :-)
Part of PCI-3617