From e7d9340d61914c6975e125d3a23bddbf9494685a Mon Sep 17 00:00:00 2001 From: tatang26 Date: Mon, 24 Feb 2025 16:20:50 -0500 Subject: [PATCH 1/3] task: allowing Procfile with comments --- dev/internal/rebuilder/procfile.go | 16 ++++- dev/internal/rebuilder/rebuilder_test.go | 86 ++++++++++++++++++++++++ dev/internal/rebuilder/writer.go | 10 ++- 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/dev/internal/rebuilder/procfile.go b/dev/internal/rebuilder/procfile.go index 9fc42a4..728b704 100644 --- a/dev/internal/rebuilder/procfile.go +++ b/dev/internal/rebuilder/procfile.go @@ -30,6 +30,15 @@ func readProcfile(path string) ([]entry, error) { continue } + for i := range parts { + parts[i] = strings.TrimSpace(parts[i]) + } + + // Skipping comments in Procfile + if strings.HasPrefix(parts[0], "#") { + continue + } + exists := slices.ContainsFunc(entries, func(e entry) bool { return e.Name == parts[0] }) @@ -38,11 +47,14 @@ func readProcfile(path string) ([]entry, error) { continue } - entries = append(entries, entry{ + e := entry{ ID: len(entries), Name: strings.TrimSpace(parts[0]), Command: strings.TrimSpace(parts[1]), - }) + } + + entries = append(entries, e) + maxServiceNameLen = max(maxServiceNameLen, len(e.Name)) } if err := scanner.Err(); err != nil { diff --git a/dev/internal/rebuilder/rebuilder_test.go b/dev/internal/rebuilder/rebuilder_test.go index be6c60c..5c26b07 100644 --- a/dev/internal/rebuilder/rebuilder_test.go +++ b/dev/internal/rebuilder/rebuilder_test.go @@ -347,4 +347,90 @@ func TestServe(t *testing.T) { t.Errorf("Expected an error, got nil") } }) + + t.Run("Validate log trailing spaces", func(t *testing.T) { + r, w, _ := os.Pipe() + + stdOut := os.Stdout + stdErr := os.Stderr + + os.Stdout = w + os.Stderr = w + t.Cleanup(func() { + os.Stdout = stdOut + os.Stderr = stdErr + }) + + procfile() + os.WriteFile("Procfile", []byte("123: echo '123'\n1234: echo '1234'\n12345: echo '12345'"), 0o644) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() + + if err := rebuilder.Serve(ctx); err != nil { + t.Errorf("Expected nil, got '%v'", err) + } + + w.Close() + var buf bytes.Buffer + io.Copy(&buf, r) + + if !strings.Contains(buf.String(), "123 |") { + t.Errorf("Expected '123 |' to be in the output, got '%v'", buf.String()) + } + + if !strings.Contains(buf.String(), "1234 |") { + t.Errorf("Expected '1234 |' to be in the output, got '%v'", buf.String()) + } + + if !strings.Contains(buf.String(), "12345 |") { + t.Errorf("Expected '12345 |' to be in the output, got '%v'", buf.String()) + } + }) + + t.Run("Read Procfile with comments", func(t *testing.T) { + r, w, _ := os.Pipe() + + stdOut := os.Stdout + stdErr := os.Stderr + + os.Stdout = w + os.Stderr = w + t.Cleanup(func() { + os.Stdout = stdOut + os.Stderr = stdErr + }) + + procfile() + + content := ` + # This is my awesome comment for my first command. + + # This command 'hello' will print a friendly 'Hello, world!' + hello: echo 'Hello, world!' + + # This command is commented because won't be used + # bye: echo 'Bye!'` + + os.WriteFile("Procfile", []byte(content), 0o644) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() + + if err := rebuilder.Serve(ctx); err != nil { + t.Errorf("Expected nil, got '%v'", err) + } + + w.Close() + var buf bytes.Buffer + io.Copy(&buf, r) + + if strings.Count(buf.String(), "hello |") != 3 { + t.Errorf("Expected 'hello |' to be in the output, got '%v'", buf.String()) + } + + if strings.Contains(buf.String(), "bye") { + t.Errorf("Expected 'bye' to be commented, got '%v'", buf.String()) + } + }) } diff --git a/dev/internal/rebuilder/writer.go b/dev/internal/rebuilder/writer.go index 75bd34d..bb2a913 100644 --- a/dev/internal/rebuilder/writer.go +++ b/dev/internal/rebuilder/writer.go @@ -10,7 +10,9 @@ import ( "time" ) -var maxServiceNameLen = 4 +// MaxServiceNameLen will receive the max length of the command name. +// This will be used to calculate the trailing spaces. +var maxServiceNameLen int var colors = []string{ "\033[92m", // Green @@ -38,7 +40,7 @@ func (cw *customWriter) Write(p []byte) (int, error) { defer cw.mu.Unlock() timestamp := time.Now().Format(time.DateTime) - trailingSpaces := strings.Repeat(" ", max(maxServiceNameLen-len(cw.prefix), 0)) + trailingSpaces := strings.Repeat(" ", max(maxServiceNameLen-len(cw.prefix), 0)+1) scanner := bufio.NewScanner(bytes.NewReader(p)) for scanner.Scan() { @@ -56,6 +58,10 @@ func (cw *customWriter) Write(p []byte) (int, error) { } } + if err := scanner.Err(); err != nil { + return 0, err + } + return len(p), nil } From 98b0786ed525f6fcb000971becbe528c37a15e95 Mon Sep 17 00:00:00 2001 From: tatang26 Date: Mon, 24 Feb 2025 17:04:26 -0500 Subject: [PATCH 2/3] task: removing '[kit] Starting app' message --- dev/internal/rebuilder/rebuilder.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/dev/internal/rebuilder/rebuilder.go b/dev/internal/rebuilder/rebuilder.go index 990b49f..74507e3 100644 --- a/dev/internal/rebuilder/rebuilder.go +++ b/dev/internal/rebuilder/rebuilder.go @@ -14,8 +14,6 @@ func Serve(ctx context.Context) error { return err } - fmt.Println("[kit] Starting app") - ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) defer cancel() From 3a351b2fc39b459e182ddb3d51cd1a054318afdf Mon Sep 17 00:00:00 2001 From: tatang26 Date: Mon, 24 Feb 2025 17:29:01 -0500 Subject: [PATCH 3/3] task: removing '[kit] Shutting down...' message --- dev/internal/rebuilder/rebuilder.go | 3 --- dev/internal/rebuilder/rebuilder_test.go | 16 ++++------------ 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/dev/internal/rebuilder/rebuilder.go b/dev/internal/rebuilder/rebuilder.go index 74507e3..0c06e84 100644 --- a/dev/internal/rebuilder/rebuilder.go +++ b/dev/internal/rebuilder/rebuilder.go @@ -2,7 +2,6 @@ package rebuilder import ( "context" - "fmt" "os" "os/signal" "syscall" @@ -34,7 +33,5 @@ func Serve(ctx context.Context) error { <-exitCh } - fmt.Println("[kit] Shutting down...") - return nil } diff --git a/dev/internal/rebuilder/rebuilder_test.go b/dev/internal/rebuilder/rebuilder_test.go index 5c26b07..60cfc1b 100644 --- a/dev/internal/rebuilder/rebuilder_test.go +++ b/dev/internal/rebuilder/rebuilder_test.go @@ -71,12 +71,8 @@ func TestServe(t *testing.T) { var buf bytes.Buffer io.Copy(&buf, r) - if !strings.Contains(buf.String(), "[kit] Starting app") { - t.Errorf("Expected '[kit] Starting app' to be in the output, got '%v'", buf.String()) - } - - if !strings.Contains(buf.String(), "[kit] Shutting down...") { - t.Errorf("Expected '[kit] Shutting down...' to be in the output, got '%v'", buf.String()) + if !strings.Contains(buf.String(), "web |") { + t.Errorf("Expected 'web |' to be in the output, got '%v'", buf.String()) } }) @@ -118,17 +114,13 @@ func TestServe(t *testing.T) { var buf bytes.Buffer io.Copy(&buf, r) - if !strings.Contains(buf.String(), "[kit] Starting app") { - t.Errorf("Expected '[kit] Starting app' to be in the output, got '%v'", buf.String()) + if !strings.Contains(buf.String(), "web |") { + t.Errorf("Expected 'web |' to be in the output, got '%v'", buf.String()) } if !strings.Contains(buf.String(), "Restarted...") { t.Errorf("Expected 'Restarted...' to be in the output, got '%v'", buf.String()) } - - if !strings.Contains(buf.String(), "[kit] Shutting down...") { - t.Errorf("Expected '[kit] Shutting down...' to be in the output, got '%v'", buf.String()) - } }) t.Run("Correct - Skip invalid commands in Procfile", func(t *testing.T) {