Skip to content

Commit

Permalink
profile: don't assume README.md is available
Browse files Browse the repository at this point in the history
Fixes #39

Don't assume README.md is in the current directly. Instead create a
temporary file to defeat the profile path test.
  • Loading branch information
davecheney committed May 9, 2017
1 parent 06b9068 commit 52c30d7
Showing 1 changed file with 98 additions and 93 deletions.
191 changes: 98 additions & 93 deletions profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ import (

type checkFn func(t *testing.T, stdout, stderr []byte, err error)

var profileTests = []struct {
name string
code string
checks []checkFn
}{{
name: "default profile (cpu)",
code: `
func TestProfile(t *testing.T) {
f, err := ioutil.TempFile("", "profile_test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())

var profileTests = []struct {
name string
code string
checks []checkFn
}{{
name: "default profile (cpu)",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -29,14 +36,14 @@ func main() {
defer profile.Start().Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: cpu profiling enabled"),
NoErr,
},
}, {
name: "memory profile",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: cpu profiling enabled"),
NoErr,
},
}, {
name: "memory profile",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -45,14 +52,14 @@ func main() {
defer profile.Start(profile.MemProfile).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: memory profiling enabled"),
NoErr,
},
}, {
name: "memory profile (rate 2048)",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: memory profiling enabled"),
NoErr,
},
}, {
name: "memory profile (rate 2048)",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -61,14 +68,14 @@ func main() {
defer profile.Start(profile.MemProfileRate(2048)).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: memory profiling enabled (rate 2048)"),
NoErr,
},
}, {
name: "double start",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: memory profiling enabled (rate 2048)"),
NoErr,
},
}, {
name: "double start",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -78,14 +85,14 @@ func main() {
profile.Start()
}
`,
checks: []checkFn{
NoStdout,
Stderr("cpu profiling enabled", "profile: Start() already called"),
Err,
},
}, {
name: "block profile",
code: `
checks: []checkFn{
NoStdout,
Stderr("cpu profiling enabled", "profile: Start() already called"),
Err,
},
}, {
name: "block profile",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -94,14 +101,14 @@ func main() {
defer profile.Start(profile.BlockProfile).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: block profiling enabled"),
NoErr,
},
}, {
name: "mutex profile",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: block profiling enabled"),
NoErr,
},
}, {
name: "mutex profile",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -110,14 +117,14 @@ func main() {
defer profile.Start(profile.MutexProfile).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: mutex profiling enabled"),
NoErr,
},
}, {
name: "profile path",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: mutex profiling enabled"),
NoErr,
},
}, {
name: "profile path",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -126,30 +133,30 @@ func main() {
defer profile.Start(profile.ProfilePath(".")).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: cpu profiling enabled, cpu.pprof"),
NoErr,
},
}, {
name: "profile path error",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: cpu profiling enabled, cpu.pprof"),
NoErr,
},
}, {
name: "profile path error",
code: `
package main
import "github.com/pkg/profile"
func main() {
defer profile.Start(profile.ProfilePath("README.md")).Stop()
defer profile.Start(profile.ProfilePath("` + f.Name() + `")).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("could not create initial output"),
Err,
},
}, {
name: "multiple profile sessions",
code: `
checks: []checkFn{
NoStdout,
Stderr("could not create initial output"),
Err,
},
}, {
name: "multiple profile sessions",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -162,23 +169,23 @@ func main() {
profile.Start(profile.MutexProfile).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: cpu profiling enabled",
"profile: cpu profiling disabled",
"profile: memory profiling enabled",
"profile: memory profiling disabled",
"profile: block profiling enabled",
"profile: block profiling disabled",
"profile: cpu profiling enabled",
"profile: cpu profiling disabled",
"profile: mutex profiling enabled",
"profile: mutex profiling disabled"),
NoErr,
},
}, {
name: "profile quiet",
code: `
checks: []checkFn{
NoStdout,
Stderr("profile: cpu profiling enabled",
"profile: cpu profiling disabled",
"profile: memory profiling enabled",
"profile: memory profiling disabled",
"profile: block profiling enabled",
"profile: block profiling disabled",
"profile: cpu profiling enabled",
"profile: cpu profiling disabled",
"profile: mutex profiling enabled",
"profile: mutex profiling disabled"),
NoErr,
},
}, {
name: "profile quiet",
code: `
package main
import "github.com/pkg/profile"
Expand All @@ -187,10 +194,8 @@ func main() {
defer profile.Start(profile.Quiet).Stop()
}
`,
checks: []checkFn{NoStdout, NoStderr, NoErr},
}}

func TestProfile(t *testing.T) {
checks: []checkFn{NoStdout, NoStderr, NoErr},
}}
for _, tt := range profileTests {
t.Log(tt.name)
stdout, stderr, err := runTest(t, tt.code)
Expand Down

0 comments on commit 52c30d7

Please sign in to comment.