Skip to content

Commit 19fc447

Browse files
Merge pull request #5 from AlexsanderHamir/refactors
Refactors
2 parents f640d72 + f0eabb2 commit 19fc447

File tree

8 files changed

+71
-9
lines changed

8 files changed

+71
-9
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
tasks.md
2-
*.out
3-
*.prof
42
*prof
53
/Enviroment*/

collector/api.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ func RunCollector(files []string, tag string) error {
4141
}
4242

4343
for _, binaryFilePath := range files {
44-
fileName := strings.TrimSuffix(binaryFilePath, filepath.Ext(binaryFilePath))
45-
profilepath := path.Join(tagDir, fileName)
46-
if err = ensureDirExists(profilepath); err != nil {
44+
binaryDirName := filepath.Base(binaryFilePath)
45+
fileName := strings.TrimSuffix(binaryDirName, filepath.Ext(binaryDirName))
46+
profileDirPath := path.Join(tagDir, fileName)
47+
if err = ensureDirExists(profileDirPath); err != nil {
4748
return err
4849
}
4950

@@ -54,7 +55,7 @@ func RunCollector(files []string, tag string) error {
5455
}
5556
}
5657

57-
outputTextFilePath := path.Join(profilepath, fileName+"."+shared.TextExtension)
58+
outputTextFilePath := path.Join(profileDirPath, fileName+"."+shared.TextExtension)
5859
if err = GenerateProfileTextOutput(binaryFilePath, outputTextFilePath); err != nil {
5960
return err
6061
}
@@ -65,7 +66,7 @@ func RunCollector(files []string, tag string) error {
6566
return fmt.Errorf("failed to extract function names: %w", err)
6667
}
6768

68-
functionDir := path.Join(profilepath, "functions")
69+
functionDir := path.Join(profileDirPath, "functions")
6970
if err = ensureDirExists(functionDir); err != nil {
7071
return err
7172
}

tests/assets/block.out

1.53 KB
Binary file not shown.

tests/assets/cpu.out

3.49 KB
Binary file not shown.

tests/assets/memory.out

1.74 KB
Binary file not shown.

tests/assets/mutex.out

327 Bytes
Binary file not shown.

tests/blackbox_test.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package tests
22

33
import (
4+
"bytes"
45
"fmt"
6+
"os"
7+
"os/exec"
8+
"path"
59
"testing"
610

711
"github.com/AlexsanderHamir/prof/config"
12+
"github.com/AlexsanderHamir/prof/shared"
813
)
914

1015
func TestConfig(t *testing.T) {
@@ -27,8 +32,6 @@ func TestConfig(t *testing.T) {
2732
},
2833
}
2934

30-
fmt.Println(defaultRunCmd())
31-
3235
testArgs := &TestArgs{
3336
specifiedFiles: specifiedFiles,
3437
cfg: cfg,
@@ -296,3 +299,52 @@ func TestCommandValidation(t *testing.T) {
296299
testConfigScenario(t, testArgs)
297300
})
298301
}
302+
303+
func TestManualCommand(t *testing.T) {
304+
root, err := getProjectRoot()
305+
if err != nil {
306+
t.Log(err)
307+
}
308+
binaryPath := path.Join(root, testDirName, "prof")
309+
310+
buildProf(t, binaryPath, root)
311+
312+
t.Cleanup(func() {
313+
benchPath := path.Join(root, testDirName, shared.MainDirOutput)
314+
if err = os.RemoveAll(benchPath); err != nil {
315+
t.Logf("Failed to clean up bench: %v", err)
316+
}
317+
318+
if err = os.Remove("prof"); err != nil {
319+
t.Logf("failed to clean prof binary: %s", err)
320+
}
321+
})
322+
323+
label := "BasicRun"
324+
t.Run(label, func(t *testing.T) {
325+
args := []string{
326+
"manual",
327+
"--tag", tag,
328+
"assets/cpu.out",
329+
"assets/memory.out",
330+
"assets/block.out",
331+
"assets/mutex.out",
332+
}
333+
334+
cmd := exec.Command("./prof", args...)
335+
cmd.Dir = path.Join(root, testDirName)
336+
337+
var stdout, stderr bytes.Buffer
338+
cmd.Stdout = &stdout
339+
cmd.Stderr = &stderr
340+
341+
err = cmd.Run()
342+
if err != nil {
343+
t.Error(err)
344+
}
345+
346+
if stdout.Len() > 0 {
347+
fmt.Println(stdout.String())
348+
}
349+
})
350+
}

tests/helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,14 @@ func defaultRunCmd() []string {
436436
"--tag", tag,
437437
}
438438
}
439+
440+
func buildProf(t *testing.T, outputPath, root string) {
441+
cmdProfDir := filepath.Join(root, "cmd", "prof")
442+
buildCmd := exec.Command("go", "build", "-o", outputPath, ".")
443+
buildCmd.Dir = cmdProfDir
444+
445+
buildOutput, err := buildCmd.CombinedOutput()
446+
if err != nil {
447+
t.Fatalf("failed to build prof binary: %v\nOutput: %s", err, buildOutput)
448+
}
449+
}

0 commit comments

Comments
 (0)