Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies for excluded modules #702

Merged
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
16 changes: 16 additions & 0 deletions .chloggen/update-dependencies-for-excluded-modules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
component: multimod

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "`prerelease` command will now update the dependencies for excluded modules."

# One or more tracking issues related to the change
issues: [702]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
3 changes: 2 additions & 1 deletion multimod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ When creating a or editing a `versions.yaml` file...
* For each module set, give it a name and specify its version and modules it
includes.
* Specify the Go module's import path (rather than its file path)
* Specify modules which should be excluded from versioning
* Specify modules which should be excluded from versioning (excluded modules
still get version update for its dependencies).
* Ensure versions are consistent with the semver versioning requirements.
* Update version numbers or module groupings as needed for new releases.

Expand Down
58 changes: 58 additions & 0 deletions multimod/internal/common/all_mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"fmt"
"io/fs"
"os"
"path/filepath"

"golang.org/x/mod/modfile"
)

func newAllModulePathMap(root string) (ModulePathMap, error) {
modPathMap := make(ModulePathMap)

findGoMod := func(filePath string, _ fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("Warning: file could not be read during filepath.Walk(): %v", err)
return nil
}

Check warning on line 33 in multimod/internal/common/all_mod.go

View check run for this annotation

Codecov / codecov/patch

multimod/internal/common/all_mod.go#L31-L33

Added lines #L31 - L33 were not covered by tests
if filepath.Base(filePath) == "go.mod" {
// read go.mod file into mod []byte
mod, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return err
}

Check warning on line 39 in multimod/internal/common/all_mod.go

View check run for this annotation

Codecov / codecov/patch

multimod/internal/common/all_mod.go#L38-L39

Added lines #L38 - L39 were not covered by tests

// read path of module from go.mod file
modPathString := modfile.ModulePath(mod)

// convert modPath, filePath string to modulePath and moduleFilePath
modPath := ModulePath(modPathString)
modFilePath := ModuleFilePath(filePath)

modPathMap[modPath] = modFilePath
}
return nil
}

if err := filepath.Walk(root, findGoMod); err != nil {
return nil, err
}

Check warning on line 55 in multimod/internal/common/all_mod.go

View check run for this annotation

Codecov / codecov/patch

multimod/internal/common/all_mod.go#L54-L55

Added lines #L54 - L55 were not covered by tests

return modPathMap, nil
}
50 changes: 50 additions & 0 deletions multimod/internal/common/all_mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/build-tools/multimod/internal/common/commontest"
)

func TestNewAllModulePathMap(t *testing.T) {
tmpRootDir := t.TempDir()
modFiles := map[string][]byte{
filepath.Join(tmpRootDir, "test", "test1", "go.mod"): []byte("module \"go.opentelemetry.io/test/test1\"\n\ngo 1.16\n\n" +
"require (\n\t\"go.opentelemetry.io/testroot/v2\" v2.0.0\n)\n"),
filepath.Join(tmpRootDir, "test", "go.mod"): []byte("module go.opentelemetry.io/test3\n\ngo 1.16\n"),
filepath.Join(tmpRootDir, "go.mod"): []byte("module go.opentelemetry.io/testroot/v2\n\ngo 1.16\n"),
filepath.Join(tmpRootDir, "test", "test2", "go.mod"): []byte("module \"go.opentelemetry.io/test/testexcluded\"\n\ngo 1.16\n"),
}

require.NoError(t, commontest.WriteTempFiles(modFiles), "could not create go mod file tree")

expected := ModulePathMap{
"go.opentelemetry.io/test/test1": ModuleFilePath(filepath.Join(tmpRootDir, "test", "test1", "go.mod")),
"go.opentelemetry.io/test3": ModuleFilePath(filepath.Join(tmpRootDir, "test", "go.mod")),
"go.opentelemetry.io/testroot/v2": ModuleFilePath(filepath.Join(tmpRootDir, "go.mod")),
"go.opentelemetry.io/test/testexcluded": ModuleFilePath(filepath.Join(tmpRootDir, "test", "test2", "go.mod")),
}

result, err := newAllModulePathMap(tmpRootDir)

require.NoError(t, err)
assert.Equal(t, expected, result)
}
14 changes: 11 additions & 3 deletions multimod/internal/common/module_set_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
// ModuleSetRelease contains info about a specific set of modules in the versioning file to be updated.
type ModuleSetRelease struct {
ModuleVersioning
ModSetName string
ModSet ModuleSet
TagNames []ModuleTagName
ModSetName string
ModSet ModuleSet
TagNames []ModuleTagName
AllModPathMap ModulePathMap
}

// NewModuleSetRelease returns a ModuleSetRelease struct by specifying a specific set of modules to update.
Expand Down Expand Up @@ -58,11 +59,18 @@
return ModuleSetRelease{}, fmt.Errorf("could not retrieve tag names from module paths: %w", err)
}

// get all module paths
allModPathMap, err := newAllModulePathMap(repoRoot)
if err != nil {
return ModuleSetRelease{}, fmt.Errorf("could not retrieve all module paths: %w", err)
}

Check warning on line 66 in multimod/internal/common/module_set_release.go

View check run for this annotation

Codecov / codecov/patch

multimod/internal/common/module_set_release.go#L65-L66

Added lines #L65 - L66 were not covered by tests

return ModuleSetRelease{
ModuleVersioning: modVersioning,
ModSetName: modSetToUpdate,
ModSet: modSet,
TagNames: tagNames,
AllModPathMap: allModPathMap,
}, nil

}
Expand Down
40 changes: 7 additions & 33 deletions multimod/internal/common/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

import (
"fmt"
"io/fs"
"os"
"path/filepath"

"github.com/spf13/viper"
"golang.org/x/mod/modfile"
)

const (
Expand Down Expand Up @@ -162,37 +158,15 @@

// BuildModulePathMap creates a map with module paths as keys and go.mod file paths as values.
func (versionCfg versionConfig) BuildModulePathMap(root string) (ModulePathMap, error) {
modPathMap := make(ModulePathMap)

findGoMod := func(filePath string, _ fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("Warning: file could not be read during filepath.Walk(): %v", err)
return nil
}
if filepath.Base(filePath) == "go.mod" {
// read go.mod file into mod []byte
mod, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return err
}

// read path of module from go.mod file
modPathString := modfile.ModulePath(mod)

// convert modPath, filePath string to modulePath and moduleFilePath
modPath := ModulePath(modPathString)
modFilePath := ModuleFilePath(filePath)

excludedModules := versionCfg.getExcludedModules()
if _, shouldExclude := excludedModules[modPath]; !shouldExclude {
modPathMap[modPath] = modFilePath
}
}
return nil
modPathMap, err := newAllModulePathMap(root)
if err != nil {
return nil, err

Check warning on line 163 in multimod/internal/common/versions.go

View check run for this annotation

Codecov / codecov/patch

multimod/internal/common/versions.go#L163

Added line #L163 was not covered by tests
}

if err := filepath.Walk(root, findGoMod); err != nil {
return nil, err
for k := range modPathMap {
if versionCfg.shouldExcludeModule(k) {
delete(modPathMap, k)
}
}

return modPathMap, nil
Expand Down
2 changes: 1 addition & 1 deletion multimod/internal/prerelease/prerelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func updateVersionGoFile(filePath string, newVersion string) error {
func (p prerelease) updateAllGoModFiles() error {
modFilePaths := make([]common.ModuleFilePath, 0, len(p.ModuleSetRelease.ModuleVersioning.ModPathMap))

for _, filePath := range p.ModuleSetRelease.ModuleVersioning.ModPathMap {
for _, filePath := range p.ModuleSetRelease.AllModPathMap {
modFilePaths = append(modFilePaths, filePath)
}

Expand Down
28 changes: 28 additions & 0 deletions multimod/internal/prerelease/prerelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test3 v0.1.0-OLD\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
")"),
filepath.Join("excluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n" +
")"),
},
},
{
Expand Down Expand Up @@ -359,6 +366,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test3 v0.1.0\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
")"),
filepath.Join("excluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test2 v1.2.3-OLD\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n" +
")"),
},
},
{
Expand Down Expand Up @@ -394,6 +408,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test3 v0.1.0-OLD\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
")"),
filepath.Join("excluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test2 v1.2.3-OLD\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n" +
")"),
},
},
}
Expand Down Expand Up @@ -434,6 +455,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test3 v0.1.0-OLD\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
")"),
filepath.Join(tmpRootDir, "excluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test2 v1.2.3-OLD\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n" +
")"),
}

require.NoError(t, commontest.WriteTempFiles(modFiles), "could not create go mod file tree")
Expand Down
Loading