Skip to content

Commit f6a8c78

Browse files
committed
Preserve indentation
1 parent e863c56 commit f6a8c78

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

tools/flakeguard/golang/golang.go

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,10 @@ func SkipTests(repoPath string, testsToSkip []*SkipTest) error {
279279
return fmt.Errorf("error parsing file '%s': %w", path, err)
280280
}
281281

282-
found = skipTestSimple(path, fileAst, fset, testToSkip)
283-
if found {
282+
skipTestSimple(path, fileAst, fset, testToSkip)
283+
// Let the outer loop know that we found the test to skip
284+
found = testToSkip.SimplySkipped || testToSkip.AlreadySkipped
285+
if testToSkip.SimplySkipped {
284286
log.Debug().
285287
Str("test", testToSkip.Name).
286288
Str("file", testToSkip.File).
@@ -290,6 +292,15 @@ func SkipTests(repoPath string, testsToSkip []*SkipTest) error {
290292

291293
return filepath.SkipDir // stop walking
292294
}
295+
if testToSkip.AlreadySkipped {
296+
log.Debug().
297+
Str("test", testToSkip.Name).
298+
Str("file", testToSkip.File).
299+
Int("line", testToSkip.Line).
300+
Str("package", testToSkip.Package).
301+
Msg("Test was already skipped")
302+
return filepath.SkipDir // stop walking
303+
}
293304
return nil
294305
})
295306
if err != nil {
@@ -319,7 +330,8 @@ func SkipTests(repoPath string, testsToSkip []*SkipTest) error {
319330

320331
// skipTestSimple parses through the file AST and skips the test or subtest if it is found
321332
// This works for most simple cases, is fast and efficient, but can't handle more complex cases like when subtests or helper functions get involved
322-
func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToSkip *SkipTest) (skipped bool) {
333+
// Results of the skip are stored in the testToSkip struct, which must be inspected after calling this function
334+
func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToSkip *SkipTest) {
323335
var (
324336
parentTest = testToSkip.Name
325337
subTest string
@@ -333,8 +345,10 @@ func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToS
333345

334346
if os.Getenv("JIRA_DOMAIN") != "" && testToSkip.JiraTicket != "" {
335347
jiraMsg = fmt.Sprintf("Skipped by flakeguard: https://%s/issues/%s", os.Getenv("JIRA_DOMAIN"), testToSkip.JiraTicket)
336-
} else {
348+
} else if testToSkip.JiraTicket != "" {
337349
jiraMsg = fmt.Sprintf("Skipped by flakeguard: %s", testToSkip.JiraTicket)
350+
} else {
351+
jiraMsg = "Skipped by flakeguard: No associated Jira ticket!"
338352
}
339353

340354
ast.Inspect(fileAst, func(n ast.Node) bool {
@@ -356,7 +370,8 @@ func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToS
356370
if selExpr, ok := callExpr.Fun.(*ast.SelectorExpr); ok {
357371
if strings.HasPrefix(selExpr.Sel.Name, "Skip") {
358372
// Test is already skipped, don't modify it
359-
skipped = true
373+
testToSkip.File = file
374+
testToSkip.Line = fset.Position(fn.Pos()).Line
360375
testToSkip.AlreadySkipped = true
361376
return false
362377
}
@@ -368,7 +383,6 @@ func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToS
368383
testToSkip.File = file
369384
testToSkip.Line = fset.Position(fn.Pos()).Line
370385
testToSkip.SimplySkipped = true
371-
skipped = true
372386
return false
373387
} else { // Loop through the body of the parent test and try to find the subtest
374388
ast.Inspect(fn.Body, func(n ast.Node) bool {
@@ -396,7 +410,8 @@ func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToS
396410
if callExpr, ok := exprStmt.X.(*ast.CallExpr); ok {
397411
if selExpr, ok := callExpr.Fun.(*ast.SelectorExpr); ok {
398412
if strings.HasPrefix(selExpr.Sel.Name, "Skip") {
399-
skipped = true
413+
testToSkip.File = file
414+
testToSkip.Line = fset.Position(fnLit.Pos()).Line
400415
testToSkip.AlreadySkipped = true
401416
return false
402417
}
@@ -408,7 +423,6 @@ func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToS
408423
testToSkip.File = file
409424
testToSkip.Line = fset.Position(fnLit.Pos()).Line
410425
testToSkip.SimplySkipped = true
411-
skipped = true
412426
return false
413427
}
414428
}
@@ -422,26 +436,56 @@ func skipTestSimple(file string, fileAst *ast.File, fset *token.FileSet, testToS
422436
return true
423437
})
424438

425-
if skipped {
439+
if testToSkip.SimplySkipped {
426440
// Add the skip statement to the test by directly inserting it into the file strings
427441
// We don't do it through the AST because it will try to do a bunch of formatting, which will mess with git diffs
428442
// This is a bit of a hack, but it works
429443
fileContent, err := os.ReadFile(file)
430444
if err != nil {
431445
testToSkip.ErrorSkipping = fmt.Errorf("failed to read file: %w", err)
432-
return false
446+
return
433447
}
434448

435-
skipStmt := fmt.Sprintf("%s.Skip(\"%s\")", tSymbol, jiraMsg)
436449
lines := strings.Split(string(fileContent), "\n")
437-
lines[testToSkip.Line-1] = fmt.Sprintf("%s\n%s", lines[testToSkip.Line-1], skipStmt)
438-
newContent := strings.Join(lines, "\n")
450+
targetLineIndex := testToSkip.Line - 1
451+
452+
// Extract indentation from the target line or the next line with content
453+
var indentation string
454+
for i := targetLineIndex; i < len(lines); i++ {
455+
line := lines[i]
456+
if strings.TrimSpace(line) != "" {
457+
// Extract leading whitespace
458+
for _, char := range line {
459+
if char == ' ' || char == '\t' {
460+
indentation += string(char)
461+
} else {
462+
break
463+
}
464+
}
465+
// Add one more level of indentation (assume tabs, but handle spaces too)
466+
if strings.Contains(indentation, "\t") {
467+
indentation += "\t"
468+
} else {
469+
indentation += " " // 4 spaces as fallback
470+
}
471+
break
472+
}
473+
}
474+
475+
skipStmt := fmt.Sprintf("%s%s.Skip(\"%s\")", indentation, tSymbol, jiraMsg)
476+
477+
// Insert the skip statement after the target line
478+
newLines := make([]string, 0, len(lines)+1)
479+
newLines = append(newLines, lines[:targetLineIndex+1]...)
480+
newLines = append(newLines, skipStmt)
481+
newLines = append(newLines, lines[targetLineIndex+1:]...)
482+
483+
newContent := strings.Join(newLines, "\n")
439484
if err := os.WriteFile(file, []byte(newContent), 0644); err != nil {
440485
testToSkip.ErrorSkipping = fmt.Errorf("failed to write file: %w", err)
441-
return false
486+
return
442487
}
443488
}
444-
return skipped
445489
}
446490

447491
/* WARNING: This was a fun experiment, but it gave massively disappointing results.

0 commit comments

Comments
 (0)