11package workflow
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
67 "strings"
@@ -16,18 +17,18 @@ func TestExtractStopTimeFromLockFile(t *testing.T) {
1617 expectedTime string
1718 }{
1819 {
19- name : "valid stop-time in lock file " ,
20+ name : "valid stop-time in GH_AW_STOP_TIME format " ,
2021 lockContent : `name: Test Workflow
2122on:
2223 workflow_dispatch:
2324jobs:
24- safety_checks :
25+ stop_time_check :
2526 runs-on: ubuntu-latest
2627 steps:
27- - name: Safety checks
28- run: |
29- STOP_TIME=" 2025-12-31 23:59:59"
30- echo "Checking stop-time limit: $STOP_TIME "` ,
28+ - uses: actions/github-script@v8
29+ env:
30+ GH_AW_STOP_TIME: 2025-12-31 23:59:59
31+ GH_AW_WORKFLOW_NAME: "Test Workflow "` ,
3132 expectedTime : "2025-12-31 23:59:59" ,
3233 },
3334 {
@@ -44,32 +45,20 @@ jobs:
4445 expectedTime : "" ,
4546 },
4647 {
47- name : "malformed stop-time line " ,
48+ name : "GH_AW_STOP_TIME with extra whitespace " ,
4849 lockContent : `name: Test Workflow
4950on:
5051 workflow_dispatch:
5152jobs:
52- safety_checks :
53+ stop_time_check :
5354 runs-on: ubuntu-latest
5455 steps:
55- - name: Safety checks
56- run: |
57- STOP_TIME=malformed-no-quotes` ,
58- expectedTime : "" ,
56+ - uses: actions/github-script@v8
57+ env:
58+ GH_AW_STOP_TIME: 2025-06-01 12:00:00
59+ GH_AW_WORKFLOW_NAME: "Test Workflow"` ,
60+ expectedTime : "2025-06-01 12:00:00" ,
5961 },
60- {
61- name : "multiple stop-time lines (should get first)" ,
62- lockContent : `name: Test Workflow
63- on:
64- workflow_dispatch:
65- jobs:
66- safety_checks:
67- runs-on: ubuntu-latest
68- steps:
69- - name: Safety checks
70- run: |
71- STOP_TIME="2025-06-01 12:00:00"
72- echo "Checking stop-time limit: $STOP_TIME"
7362 STOP_TIME = "2025-07-01 12:00:00" `,
7463 expectedTime: "2025-06-01 12:00:00",
7564 },
@@ -158,3 +147,114 @@ func TestResolveStopTimeRejectsMinutes(t *testing.T) {
158147 })
159148 }
160149}
150+
151+ // TestRefreshStopTimeBehavior tests that the refreshStopTime flag controls stop time preservation
152+ func TestRefreshStopTimeBehavior(t *testing.T) {
153+ // Create a temporary directory for test files
154+ tmpDir, err := os.MkdirTemp("", "refresh-stop-time-test")
155+ if err != nil {
156+ t.Fatalf("Failed to create temp dir: %v", err)
157+ }
158+ defer os.RemoveAll(tmpDir)
159+
160+ // Create a markdown workflow file with stop-after
161+ mdFile := filepath.Join(tmpDir, "test.md")
162+ lockFile := filepath.Join(tmpDir, "test.lock.yml")
163+
164+ // Create a lock file with existing stop time
165+ existingStopTime := "2025-12-31 23:59:59"
166+ lockContent := fmt.Sprintf(` name : Test Workflow
167+ on :
168+ workflow_dispatch :
169+ jobs :
170+ stop_time_check :
171+ runs - on : ubuntu - latest
172+ steps:
173+ - uses : actions / github - script @v8
174+ env:
175+ GH_AW_STOP_TIME : % s
176+ GH_AW_WORKFLOW_NAME: "Test Workflow"
177+ `, existingStopTime)
178+ err = os.WriteFile(lockFile, []byte(lockContent), 0644)
179+ if err != nil {
180+ t.Fatalf("Failed to create lock file: %v", err)
181+ }
182+
183+ // Test 1: Default behavior should preserve existing stop time
184+ t.Run("default behavior preserves stop time", func(t *testing.T) {
185+ compiler := NewCompiler(false, "", "test")
186+ compiler.SetRefreshStopTime(false)
187+
188+ frontmatter := map[string]any{
189+ "on": map[string]any{
190+ "workflow_dispatch": nil,
191+ "stop-after": "+48h",
192+ },
193+ }
194+
195+ workflowData := &WorkflowData{}
196+ err = compiler.processStopAfterConfiguration(frontmatter, workflowData, mdFile)
197+ if err != nil {
198+ t.Fatalf("processStopAfterConfiguration failed: %v", err)
199+ }
200+
201+ if workflowData.StopTime != existingStopTime {
202+ t.Errorf("Expected stop time to be preserved as %q, got %q", existingStopTime, workflowData.StopTime)
203+ }
204+ })
205+
206+ // Test 2: With refresh flag, should generate new stop time
207+ t.Run("refresh flag generates new stop time", func(t *testing.T) {
208+ compiler := NewCompiler(false, "", "test")
209+ compiler.SetRefreshStopTime(true)
210+
211+ frontmatter := map[string]any{
212+ "on": map[string]any{
213+ "workflow_dispatch": nil,
214+ "stop-after": "+48h",
215+ },
216+ }
217+
218+ workflowData := &WorkflowData{}
219+ err = compiler.processStopAfterConfiguration(frontmatter, workflowData, mdFile)
220+ if err != nil {
221+ t.Fatalf("processStopAfterConfiguration failed: %v", err)
222+ }
223+
224+ if workflowData.StopTime == existingStopTime {
225+ t.Errorf("Expected stop time to be refreshed, but got the same value: %q", workflowData.StopTime)
226+ }
227+
228+ // Verify the new stop time is a valid timestamp
229+ if workflowData.StopTime == "" {
230+ t.Error("Expected stop time to be set, got empty string")
231+ }
232+ })
233+
234+ // Test 3: First compilation without existing lock file should generate new stop time
235+ t.Run("first compilation generates new stop time", func(t *testing.T) {
236+ // Remove the lock file for this test
237+ os.Remove(lockFile)
238+
239+ compiler := NewCompiler(false, "", "test")
240+ compiler.SetRefreshStopTime(false)
241+
242+ frontmatter := map[string]any{
243+ "on": map[string]any{
244+ "workflow_dispatch": nil,
245+ "stop-after": "+48h",
246+ },
247+ }
248+
249+ workflowData := &WorkflowData{}
250+ err = compiler.processStopAfterConfiguration(frontmatter, workflowData, mdFile)
251+ if err != nil {
252+ t.Fatalf("processStopAfterConfiguration failed: %v", err)
253+ }
254+
255+ // Verify a new stop time was generated
256+ if workflowData.StopTime == "" {
257+ t.Error("Expected stop time to be set, got empty string")
258+ }
259+ })
260+ }
0 commit comments