src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/pprof/pprof_test.go (about) 1 package pprof_test 2 3 import ( 4 "os" 5 "testing" 6 7 . "src.elv.sh/pkg/pprof" 8 "src.elv.sh/pkg/prog" 9 "src.elv.sh/pkg/prog/progtest" 10 "src.elv.sh/pkg/testutil" 11 ) 12 13 var ( 14 Test = progtest.Test 15 ThatElvish = progtest.ThatElvish 16 ) 17 18 func TestProgram(t *testing.T) { 19 testutil.InTempDir(t) 20 21 Test(t, prog.Composite(&Program{}, noopProgram{}), 22 ThatElvish("-cpuprofile", "cpu").DoesNothing(), 23 ThatElvish("-cpuprofile", "bad/path"). 24 WritesStderrContaining("Warning: cannot create CPU profile:"), 25 26 ThatElvish("-allocsprofile", "allocs").DoesNothing(), 27 ThatElvish("-allocsprofile", "bad/path"). 28 WritesStderrContaining("Warning: cannot create memory allocation profile:"), 29 ) 30 31 // Check for the effects of -cpuprofile and -allocsprofile. There isn't much 32 // that can be checked easily, so we only do a sanity check that the profile 33 // files exist and are non-empty. 34 checkFileIsNonEmpty(t, "cpu") 35 checkFileIsNonEmpty(t, "allocs") 36 } 37 38 func checkFileIsNonEmpty(t *testing.T, name string) { 39 t.Helper() 40 stat, err := os.Stat(name) 41 if err != nil { 42 t.Errorf("CPU profile file does not exist: %v", err) 43 } else if stat.Size() == 0 { 44 t.Errorf("CPU profile exists but is empty") 45 } 46 } 47 48 type noopProgram struct{} 49 50 func (noopProgram) RegisterFlags(*prog.FlagSet) {} 51 func (noopProgram) Run([3]*os.File, []string) error { return nil }