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

nil *testing.T handling #1355

Open
artem-nefedov opened this issue Oct 7, 2023 · 1 comment
Open

nil *testing.T handling #1355

artem-nefedov opened this issue Oct 7, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@artem-nefedov
Copy link

Describe the bug

I've run into unclear behavior when *testing.T is nil, and I'm not sure if it's a bug or intended behavior.

I have a function that can be run from either tests or from a regular go program (in which case *testing.T doesn't exist), and that function contains shell.RunCommandAndGetOutput call. I realise this is probably a non-standard use of library, but still want to understand what's going on.

I found that this code will work fine:

	cmd := shell.Command{
		Command: "echo",
		Args: []string{"foo"},
		Logger: logger.Discard,
	}

	fmt.Println(shell.RunCommandAndGetOutput(nil, cmd))

But this code will crash will nil ptr dereference:

	cmd := shell.Command{
		Command: "echo",
		Args: []string{"foo"},
		Logger: logger.Discard,
	}

	var t *testing.T = nil
	fmt.Println(shell.RunCommandAndGetOutput(t, cmd))

Resulting in output:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x100d81a60]

goroutine 1 [running]:
testing.(*T).Helper(0x1400000e168?)
        <autogenerated>:1 +0x20
github.com/gruntwork-io/terratest/modules/logger.(*Logger).Logf(0x100f83fc0, {0x100e8cfe8, 0x0}, {0x100df8c2b, 0x1f}, {0x1400006e260, 0x2, 0x2})
        /Users/user/go/pkg/mod/github.com/gruntwork-io/terratest@v0.45.0/modules/logger/logger.go:60 +0x54
github.com/gruntwork-io/terratest/modules/shell.runCommand({0x100e8cfe8, 0x0}, {{0x100df2237, 0x4}, {0x140000122b0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...})
        /Users/user/go/pkg/mod/github.com/gruntwork-io/terratest@v0.45.0/modules/shell/command.go:100 +0x104
github.com/gruntwork-io/terratest/modules/shell.RunCommandAndGetOutputE({0x100e8cfe8?, 0x0?}, {{0x100df2237, 0x4}, {0x140000122b0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...})
        /Users/user/go/pkg/mod/github.com/gruntwork-io/terratest@v0.45.0/modules/shell/command.go:58 +0x60
github.com/gruntwork-io/terratest/modules/shell.RunCommandAndGetOutput({0x100e8cfe8?, 0x0}, {{0x100df2237, 0x4}, {0x140000122b0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...})
        /Users/user/go/pkg/mod/github.com/gruntwork-io/terratest@v0.45.0/modules/shell/command.go:49 +0x64
main.main()
        /Users/user/a/main.go:18 +0xac
exit status 2

So, to work around this, I had to write this weird-looking construct:

if t == nil {
	out = shell.RunCommandAndGetOutput(nil, cmd)
} else {
	out = shell.RunCommandAndGetOutput(t, cmd)
}

Versions

  • Terratest version: v0.45.0
  • Environment details (Ubuntu 20.04, Windows 10, etc.): Mac/Linux
@artem-nefedov artem-nefedov added the bug Something isn't working label Oct 7, 2023
@denis256
Copy link
Member

Hello,
since Terratest was intended to be used inside of Go tests, it is expected to have *testing.T passed in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants