Skip to content

Commit

Permalink
cmd/cue: implement cue mod tidy --check
Browse files Browse the repository at this point in the history
This wires up `modload.CheckTidy` to the cue command.

Specifically, `cue mod tidy --check` succeeds only if the
module file is contains exactly the required dependencies
and also contains a CUE language version.

Fixes #2910.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I49379d28d7ab156016cfeb21bd26834e42dd2df1
Dispatch-Trailer: {"type":"trybot","CL":1178021,"patchset":2,"ref":"refs/changes/21/1178021/2","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Mar 12, 2024
1 parent b31ff65 commit c2dea08
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// Common flags
const (
flagAll flagName = "all"
flagCheck flagName = "check"
flagDryrun flagName = "dryrun"
flagVerbose flagName = "verbose"
flagAllErrors flagName = "all-errors"
Expand Down
5 changes: 4 additions & 1 deletion cmd/cue/cmd/modtidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ for this command to work.
RunE: mkRunE(c, runModTidy),
Args: cobra.ExactArgs(0),
}
cmd.Flags().Bool(string(flagCheck), false, "check for tidiness only; do not update module.cue file")

return cmd
}
Expand All @@ -67,11 +68,13 @@ func runModTidy(cmd *Command, args []string) error {
if err != nil {
return err
}
if flagCheck.Bool(cmd) {
return modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg)
}
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, versionForModFile())
if err != nil {
return err
}
// TODO check whether it's changed or not.
data, err := mf.Format()
if err != nil {
return fmt.Errorf("internal error: invalid module.cue file generated: %v", err)
Expand Down
26 changes: 26 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_check.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Check that cue mod tidy --check succeeds OK
# even when the module file is formatted differently
# e.g. language.version in a single line.

exec cue mod tidy --check

-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.99.99"

// A comment.
deps: "example.com@v0": v: "v0.0.1"

-- main.cue --
package main
import "example.com@v0:main"

main

-- _registry/example.com_v0.0.1/cue.mod/module.cue --
module: "example.com@v0"

-- _registry/example.com_v0.0.1/top.cue --
package main

"example.com@v0": "v0.0.1"
25 changes: 25 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_check_fail.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Check that cue mod tidy --check fails when some dependencies aren't present.

! exec cue mod tidy --check
cmp stderr want-stderr

-- want-stderr --
module is not tidy: cannot find module providing package example.com@v0:main
-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.99.99"

-- main.cue --
package main
import "example.com@v0:main"

main

-- _registry/example.com_v0.0.1/cue.mod/module.cue --
module: "example.com@v0"

-- _registry/example.com_v0.0.1/top.cue --
package main

"example.com@v0": "v0.0.1"

3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_initial.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ cmp cue.mod/module.cue want-module
# Check that the resulting module evaluates as expected.
exec cue export .
cmp stdout want-stdout

# Check that the tidy check succeeds
exec cue mod tidy --check
-- want-module --
module: "main.org@v0"
language: {
Expand Down

0 comments on commit c2dea08

Please sign in to comment.