Skip to content

Commit

Permalink
Merge branch 'main' into feature/1105-1-theme-on-index
Browse files Browse the repository at this point in the history
  • Loading branch information
SallyMcGrath authored Oct 24, 2024
2 parents f038d7c + 141df07 commit 31e3c04
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
15 changes: 9 additions & 6 deletions org-cyf-itp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module github.com/CodeYourFuture/curriculum/org-cyf-itp
go 1.22.6

require (
github.com/CodeYourFuture/CYF-PD v1.0.1-0.20240721130916-d70fc853a278 // indirect
github.com/CodeYourFuture/curriculum/common-content v0.0.0-20241008095008-281a5517463f // indirect
github.com/CodeYourFuture/curriculum/common-theme v0.0.0-20241008095008-281a5517463f // indirect
github.com/CodeYourFuture/curriculum/org-cyf-guides v0.0.0-20240721115017-ac0d39b0bbe3 // indirect
github.com/CodeYourFuture/curriculum/org-cyf-how-this-works v0.0.0-00010101000000-000000000000 // indirect
github.com/CodeYourFuture/curriculum/org-cyf-piscine v0.0.0-20240818070728-e8702788f7cb // indirect
github.com/CodeYourFuture/CYF-PD v1.0.1-0.20240905150045-7be086ff6886 // indirect
github.com/CodeYourFuture/curriculum/common-content v0.0.0-20241022142707-ee3974f18eb2 // indirect
github.com/CodeYourFuture/curriculum/common-theme v0.0.0-20241022142707-ee3974f18eb2 // indirect
github.com/CodeYourFuture/curriculum/org-cyf-guides v0.0.0-20241022142707-ee3974f18eb2 // indirect
github.com/CodeYourFuture/curriculum/org-cyf-how-this-works v0.0.0-20241022142707-ee3974f18eb2 // indirect
github.com/CodeYourFuture/curriculum/org-cyf-piscine v0.0.0-20241022142707-ee3974f18eb2 // indirect
github.com/CodeYourFuture/curriculum/org-cyf-theme v0.0.0-20241022142707-ee3974f18eb2 // indirect
)

replace github.com/CodeYourFuture/curriculum/common-content => ../common-content
Expand All @@ -20,3 +21,5 @@ replace github.com/CodeYourFuture/curriculum/org-cyf-guides => ../org-cyf-guides
replace github.com/CodeYourFuture/curriculum/org-cyf-how-this-works => ../org-cyf-how-this-works

replace github.com/CodeYourFuture/curriculum/org-cyf-piscine => ../org-cyf-piscine

replace github.com/CodeYourFuture/curriculum/org-cyf-theme => ../org-cyf-theme
6 changes: 4 additions & 2 deletions org-cyf-itp/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/CodeYourFuture/CYF-PD v1.0.1-0.20240721130916-d70fc853a278 h1:QkyzRerSZyumZ4gxoE7gehj86zWdDY7O4K9pqW8OLtg=
github.com/CodeYourFuture/CYF-PD v1.0.1-0.20240721130916-d70fc853a278/go.mod h1:DJj2pdPceTBoHErILtnZMEYPZZuC8W9Fmt/faaW2tcw=
github.com/CodeYourFuture/CYF-PD v1.0.1-0.20240905150045-7be086ff6886 h1:ZKZYah39sNwr1VpTjl/q8mWq/0bj9X3cRGJpUHt8CKQ=
github.com/CodeYourFuture/CYF-PD v1.0.1-0.20240905150045-7be086ff6886/go.mod h1:DJj2pdPceTBoHErILtnZMEYPZZuC8W9Fmt/faaW2tcw=
github.com/CodeYourFuture/curriculum/org-cyf-theme v0.0.0-20241022142707-ee3974f18eb2 h1:GpkMPRNPE1718eMFHYouqq+0jl1dgbwMGiE6pPBDi6M=
github.com/CodeYourFuture/curriculum/org-cyf-theme v0.0.0-20241022142707-ee3974f18eb2/go.mod h1:5RMrsJNpszVsZskrrC9fmMurvjlcvU74VBcJz8I4LtA=
8 changes: 6 additions & 2 deletions tooling/go/cmd/local-overrides-enforcer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ func main() {
if err != nil {
return fmt.Errorf("failed to read %s: %w", path, err)
}
expectedContents, ok, err := checker.CheckFile(path, content, parentModule)
expectedContents, ok, missingReplaces, err := checker.CheckFile(path, content, parentModule)
if err != nil {
return fmt.Errorf("failed to check %s: %w", path, err)
}
if !ok {
sawBadFile = true
fmt.Printf("⚠️ File at path %s didn't have some local overrides - its contents should be:\n%s\n", path, expectedContents)
missingReplacesStr := ""
for _, missingReplace := range missingReplaces {
missingReplacesStr += " - " + missingReplace + "\n"
}
fmt.Printf("⚠️ File at path %s didn't have some replace directives.\nMissing replace directives for modules:\n%s\nYou should replace %s with exactly this string:\n```\n%s\n```\n", path, missingReplacesStr, path, expectedContents)
}
return nil
})
Expand Down
26 changes: 13 additions & 13 deletions tooling/go/internal/local-overrides-enforcer/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (

// CheckFile checks that a go.mod file has local overrides for all children of the passed parent module.
// It returns one of:
// - If the contents was correct: "", true, nil
// - If the contents was not correct: the expected contents, false, nil
// - If an error occurred: "", false, error
func CheckFile(goModPath string, contents []byte, parentModule string) (string, bool, error) {
// - If the contents was correct: "", true, nil, nil
// - If the contents was not correct: the expected contents, false, []string{"missing/override"...}, nil
// - If an error occurred: "", false, nil, error
func CheckFile(goModPath string, contents []byte, parentModule string) (string, bool, []string, error) {
gomodFile, err := modfile.Parse(goModPath, contents, nil)
if err != nil {
return "", false, fmt.Errorf("failed to parse %s as go.mod file: %w", goModPath, err)
return "", false, nil, fmt.Errorf("failed to parse %s as go.mod file: %w", goModPath, err)
}

parentModuleWithTrailingSlash := parentModule + "/"

if !strings.HasPrefix(gomodFile.Module.Mod.Path, parentModuleWithTrailingSlash) {
return "", false, fmt.Errorf("module at path %s was named %s which isn't a child of %s", goModPath, gomodFile.Module.Mod.Path, parentModule)
return "", false, nil, fmt.Errorf("module at path %s was named %s which isn't a child of %s", goModPath, gomodFile.Module.Mod.Path, parentModule)
}
slashCount := strings.Count(gomodFile.Module.Mod.Path[len(parentModule):], "/")

Expand All @@ -30,7 +30,7 @@ func CheckFile(goModPath string, contents []byte, parentModule string) (string,
replaces[replace.Old.Path] = struct{}{}
}

missingReplaces := false
var missingReplaces []string
for _, require := range gomodFile.Require {
modPath := require.Mod.Path
if !strings.HasPrefix(modPath, parentModuleWithTrailingSlash) {
Expand All @@ -39,18 +39,18 @@ func CheckFile(goModPath string, contents []byte, parentModule string) (string,
if _, isReplaced := replaces[modPath]; isReplaced {
continue
}
missingReplaces = true
missingReplaces = append(missingReplaces, modPath)
rel := modPath[len(parentModuleWithTrailingSlash):]
if err := gomodFile.AddReplace(modPath, "", strings.Repeat("../", slashCount)+rel, ""); err != nil {
return "", false, fmt.Errorf("failed to add replace: %w", err)
return "", false, nil, fmt.Errorf("failed to add replace: %w", err)
}
}
if missingReplaces {
if len(missingReplaces) != 0 {
formatted, err := gomodFile.Format()
if err != nil {
return "", false, fmt.Errorf("failed to serialize go.mod file: %w", err)
return "", false, nil, fmt.Errorf("failed to serialize go.mod file: %w", err)
}
return string(formatted), false, nil
return string(formatted), false, missingReplaces, nil
}
return "", true, nil
return "", true, nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ require (
)
`

newContent, ok, err := checker.CheckFile("/some/go.mod", []byte(goModContent), "github.com/CodeYourFuture/curriculum")
newContent, ok, missingReplaces, err := checker.CheckFile("/some/go.mod", []byte(goModContent), "github.com/CodeYourFuture/curriculum")
require.NoError(t, err)
require.True(t, ok)
require.Empty(t, missingReplaces)
require.Equal(t, "", newContent)
}

Expand All @@ -40,9 +41,10 @@ func TestMissingReplace(t *testing.T) {
)
`

newContent, ok, err := checker.CheckFile("/some/go.mod", []byte(goModContent), "github.com/CodeYourFuture/curriculum")
newContent, ok, missingReplaces, err := checker.CheckFile("/some/go.mod", []byte(goModContent), "github.com/CodeYourFuture/curriculum")
require.NoError(t, err)
require.False(t, ok)
require.Equal(t, missingReplaces, []string{"github.com/CodeYourFuture/curriculum/common-content"})
require.Contains(t, newContent, "replace github.com/CodeYourFuture/curriculum/common-content => ../common-content")
}

Expand All @@ -60,11 +62,11 @@ require (
)
`

_, _, err := checker.CheckFile("/some/go.mod", []byte(goModContent), "github.com/CodeYourFuture/curriculum")
_, _, _, err := checker.CheckFile("/some/go.mod", []byte(goModContent), "github.com/CodeYourFuture/curriculum")
require.ErrorContains(t, err, "module at path /some/go.mod was named github.com/CodeYourFuture/wrong which isn't a child of github.com/CodeYourFuture/curriculum")
}

func TestInvalidGoModFile(t *testing.T) {
_, _, err := checker.CheckFile("/some/go.mod", []byte("hello"), "github.com/CodeYourFuture/curriculum")
_, _, _, err := checker.CheckFile("/some/go.mod", []byte("hello"), "github.com/CodeYourFuture/curriculum")
require.ErrorContains(t, err, "failed to parse /some/go.mod as go.mod file: ")
}

0 comments on commit 31e3c04

Please sign in to comment.