Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/commands/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ func (r *jobsRunner) followLogs(cmd *cobra.Command, runID string, intervalSec fl
if err != nil {
return false, err
}
for _, line := range extractLogLines(respBody) {
lines := extractLogLines(respBody)
for _, line := range lines {
if _, err := fmt.Fprintln(cmd.OutOrStdout(), line); err != nil {
return false, err
}
Expand All @@ -915,6 +916,8 @@ func (r *jobsRunner) followLogs(cmd *cobra.Command, runID string, intervalSec fl
if next != cursor {
cursor = next
}
} else {
cursor += len(lines)
}

runBody, err := r.execWithRetry(cmd.Context(), r.getRun, []string{runID}, nil, "")
Expand Down
53 changes: 53 additions & 0 deletions internal/commands/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,59 @@ func TestJobsRunUsesUploadPathEnvOverride(t *testing.T) {
}
}

func TestJobsLogsFollowDoesNotReprintWhenNextPageNull(t *testing.T) {
t.Parallel()

var logCalls int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case r.Method == http.MethodGet && r.URL.Path == "/runs/run-777/logs":
logCalls++
cursor := r.URL.Query().Get("cursor")
switch {
case cursor == "0":
// First poll: 2 lines, no next_page
_, _ = io.WriteString(w, `{"items":[{"raw":"line-1"},{"raw":"line-2"}],"current_page":0,"next_page":null}`)
case cursor == "2":
// Second poll: 1 new line, no next_page
_, _ = io.WriteString(w, `{"items":[{"raw":"line-3"}],"current_page":2,"next_page":null}`)
default:
// Any further poll: no new lines
_, _ = io.WriteString(w, `{"items":[],"current_page":0,"next_page":null}`)
}
case r.Method == http.MethodGet && r.URL.Path == "/runs/run-777":
// Terminal on first status check so we don't loop forever
_, _ = io.WriteString(w, `{"id":"run-777","status":"COMPLETED"}`)
default:
http.NotFound(w, r)
}
}))
defer server.Close()

root := buildJobsTestRoot(server.URL)
var out bytes.Buffer
root.SetOut(&out)
root.SetErr(&bytes.Buffer{})
root.SetArgs([]string{"job-runs", "logs", "run-777", "--follow", "--interval", "0.01"})

if err := root.Execute(); err != nil {
t.Fatalf("Execute() error = %v", err)
}

got := out.String()
// Each line should appear exactly once
if count := strings.Count(got, "line-1"); count != 1 {
t.Errorf("expected line-1 once, got %d times in %q", count, got)
}
if count := strings.Count(got, "line-2"); count != 1 {
t.Errorf("expected line-2 once, got %d times in %q", count, got)
}
if count := strings.Count(got, "line-3"); count != 1 {
t.Errorf("expected line-3 once, got %d times in %q", count, got)
}
}

func TestJobsLogsJsonOutput(t *testing.T) {
t.Parallel()

Expand Down
Loading