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

Run test-go-mod on local modules #4975

Closed
Closed
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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ functions-examples-all:
done

test-go-mod:
./hack/for-each-module.sh "go list -m -json all > /dev/null && go mod tidy -v"
./hack/for-each-module.sh $$(pwd)/hack/replace.sh; \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using $$KUSTOMIZE_ROOT instead of $$(pwd), but couldn't figure out how to use the exported hack/for-each-module.sh variable here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "\$$KUSTOMIZE_ROOT/hack/... should work!

./hack/for-each-module.sh "go mod tidy -v"; \
./hack/for-each-module.sh $$(pwd)/hack/dropReplace.sh
Comment on lines +155 to +157
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried inlining the scripts in replace.sh and dropReplace.sh, but couldn't figure out how to escape the relevant characters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think calling a script from here is better than inlining the details anyway. :)

I'd suggest making a single script we can invoke from here like ./hack/for-each-module.sh "\$$KUSTOMIZE_ROOT/hack/go-mod-tidy-kust-dev.sh" and then adding a bunch of comments in that file to explain the steps and why we're doing them. Or to be more flexible, we can make a script that itself takes a command to run in the context of the "pinned" module, invoked like: ./hack/for-each-module.sh "\$$KUSTOMIZE_ROOT/hack/with-pinned-kust-dev.sh 'go mod tidy -v'".


.PHONY:
verify-kustomize-e2e: $(MYGOBIN)/mdrip $(MYGOBIN)/kind
Expand Down
9 changes: 9 additions & 0 deletions hack/dropReplace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0

read -a modules <<< $(go list -m)

for i in ${!modules[@]}; do
go mod edit -dropreplace=${modules[i]}
done
14 changes: 14 additions & 0 deletions hack/replace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Copyright 2023 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0

read -a modules <<< $(go list -m)
read -a module_paths <<< $(go list -m -f {{.Dir}})
Comment on lines +5 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to move this into Makefile to avoid re-calculating it for each module, but I don't know how to export variables in Makefile so that they're accessible here.

Comment on lines +5 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming go list -m returns the modules in the same order on every call.

I've considered 2 alternatives:

  • go list -m -json returns all information, including name and directory path, for all modules, but I'd have to parse the json output
  • fetch the module name as I navigate to each module with hack/for-each-module.sh; however, go list -m lists all modules in the workspace regardless of the working directory, so the best I could come up with was parsing the go.mod file for the module name


for i in ${!modules[@]}; do
replace_path=$(realpath --relative-to=$(pwd) ${module_paths[i]})
if [ $replace_path == . ]; then
continue
fi
go mod edit -replace=${modules[i]}=$replace_path
done