Skip to content

Commit

Permalink
ci: add script to fix go test caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Jun 15, 2023
1 parent 03f6b25 commit fa00a07
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/pr_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ jobs:
name: config-artifact-${{ github.sha }}
- name: "Move Config"
run: mv config.toml pkg/config/config.toml
# NOTE: Windows should fail quietly for 'test' pre-requisite target.
# On Windows, executing `make config` works fine.
# But via GitHub Actions the ../../scripts/config.sh isn't run.
# This is because you can't nest PowerShell instances.
- name: "Modify git cloned repo files 'modified' times"
run: go run ./scripts/go-test-cache/main.go
# NOTE: Windows should fail quietly running pre-requisite target of `test`.
#
# On Windows, executing `make config` directly works fine.
# But when `config` is a pre-requisite to running `test`, it fails.
# But only when run via GitHub Actions.
# The ../../scripts/config.sh isn't run because you can't nest PowerShell instances.
# Each GitHub Action 'run' step is a PowerShell instance.
# And each instance is run as: powershell.exe -command ". '...'"
- name: "Test suite"
Expand Down
3 changes: 3 additions & 0 deletions scripts/go-test-cache/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go-test-cache

go 1.20
111 changes: 111 additions & 0 deletions scripts/go-test-cache/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This script is based on https://github.com/airplanedev/blog-examples/blob/main/go-test-caching/update_file_timestamps.py?ref=airplane.ghost.io
// and was generated automatically using AI.
package main

import (
"crypto/sha1"
"io"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
)

const (
BUF_SIZE = 65536
BASE_DATE = 1684178360
TIME_FORMAT = "2006-01-02 15:04:05"
)

func main() {
repoRoot := "."
allDirs := make([]string, 0)

err := filepath.Walk(repoRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
dirPath := filepath.Join(repoRoot, path)
relPath, _ := filepath.Rel(repoRoot, dirPath)

if strings.HasPrefix(relPath, ".") {
return nil
}

allDirs = append(allDirs, dirPath)
} else {
filePath := filepath.Join(repoRoot, path)
relPath, _ := filepath.Rel(repoRoot, filePath)

if strings.HasPrefix(relPath, ".") {
return nil
}

sha1Hash, err := getFileSHA1(filePath)
if err != nil {
return err
}

modTime := getModifiedTime(sha1Hash)

log.Printf("Setting modified time of file %s to %s\n", relPath, modTime.Format(TIME_FORMAT))
err = os.Chtimes(filePath, modTime, modTime)
if err != nil {
return err
}
}

return nil
})
if err != nil {
log.Fatal("Error:", err)
}

sort.Slice(allDirs, func(i, j int) bool {
return len(allDirs[i]) > len(allDirs[j]) || (len(allDirs[i]) == len(allDirs[j]) && allDirs[i] < allDirs[j])
})

for _, dirPath := range allDirs {
relPath, _ := filepath.Rel(repoRoot, dirPath)

log.Printf("Setting modified time of directory %s to %s\n", relPath, time.Unix(BASE_DATE, 0).Format(TIME_FORMAT))
err := os.Chtimes(dirPath, time.Unix(BASE_DATE, 0), time.Unix(BASE_DATE, 0))
if err != nil {
log.Fatal("Error:", err)
}
}

log.Println("Done")
}

func getFileSHA1(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()

hash := sha1.New()
if _, err := io.CopyBuffer(hash, file, make([]byte, BUF_SIZE)); err != nil {
return "", err
}

return string(hash.Sum(nil)), nil
}

func getModifiedTime(sha1Hash string) time.Time {
hashBytes := []byte(sha1Hash)
lastFiveBytes := hashBytes[:5]
lastFiveValue := int64(0)

for _, b := range lastFiveBytes {
lastFiveValue = (lastFiveValue << 8) + int64(b)
}

modTime := BASE_DATE - (lastFiveValue % 10000)
return time.Unix(modTime, 0)
}

0 comments on commit fa00a07

Please sign in to comment.