# cue mod tidy when a replaceWith field is present in cue.mod/module.cue.
# The replaceWith field belongs in cue.mod/local-module.cue, so tidy tolerates one
# in module.cue and migrates it: it records the replace in local-module.cue
# (creating the file) and removes it from the published module.cue.

# Before tidying, the module is not tidy because module.cue carries a replace.
! exec cue mod tidy --check
stderr 'module is not tidy'

exec cue mod tidy
cmp cue.mod/module.cue want-module
cmp cue.mod/local-module.cue want-local

# The build uses the local directory's content.
exec cue export .
cmp stdout want-stdout

# tidy is idempotent.
exec cue mod tidy --check

-- want-module --
module: "main.org@v0"
language: {
	version: "v0.17.0"
}
deps: {
	"example.com/dep@v0": {
		v: "v0.1.0"
	}
}
-- want-local --
deps: {
	"example.com/dep@v0": {
		replaceWith: "./local_dep"
	}
}
-- want-stdout --
{
    "out": "from local directory"
}
-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.17.0"
deps: "example.com/dep@v0": {
	v:       "v0.1.0"
	replaceWith: "./local_dep"
}
-- main.cue --
package main

import "example.com/dep@v0:lib"

out: lib.value
-- local_dep/cue.mod/module.cue --
module: "example.com/dep@v0"
language: version: "v0.9.0"
-- local_dep/lib.cue --
package lib

value: "from local directory"
-- _registry/example.com_dep_v0.1.0/cue.mod/module.cue --
module: "example.com/dep@v0"
language: version: "v0.9.0"
-- _registry/example.com_dep_v0.1.0/lib.cue --
package lib

value: "from registry"
