Josh
Josh

Reputation: 945

How to test system commands in go

Given I have a function like:

func getTotalMemory() string {
  out,_ := exec.Command("grep", "MemTotal", "/proc/meminfo").Output()
  t := strings.Split(string(out), ":")
  x := strings.TrimSpace(t[1])
  return x
}

How can I write a test for that function to make sure I'm parsing it properly? In ruby I would just do something like

os.expects(:Command).and_returns("string")

I'm currently using GoConvey if that has any impact on answers.

Thanks!

Upvotes: 3

Views: 2214

Answers (1)

VonC
VonC

Reputation: 1324505

You could start by looking at the test policy for os.exec package:
see "src/pkg/os/exec/exec_test.go".

Note: GoConvey is compatible with standard Go testing framework, so that won't have any bearing on the kind of test you want to do.

Upvotes: 4

Related Questions