# Test that cue mod publish succeeds when the module path lacks a major version suffix.
# Using or developing the module locally works otherwise, even `cue mod tidy`.

memregistry MEMREGISTRY
env CUE_REGISTRY=$MEMREGISTRY+insecure

exec cue export
cmp stdout export.stdout
exec cue mod tidy --check

cp cue.mod/module.cue cue.mod/module.cue.original

exec cue mod publish v0.0.1

# The inferred version is v0. Using any other major version will fail.
! exec cue mod publish v1.0.0
stderr 'publish version "v1.0.0" does not match implied major version "v0" in ".*module.cue"; must be v0.N.N'
! exec cue mod publish v2.0.0
stderr 'publish version "v2.0.0" does not match implied major version "v0" in ".*module.cue"; must be v0.N.N'

# `cue mod fix` will not add add a major version suffix when missing.
exec cue mod fix
cmp cue.mod/module.cue cue.mod/module.cue.fixed

cp cue.mod/module.cue.original cue.mod/module.cue
exec cue mod edit --module main.example@v0
cmp cue.mod/module.cue cue.mod/module.cue.edited-with-v0

# Trying to publish with the wrong major version will fail.
! exec cue mod publish v1.0.0
stderr 'publish version "v1.0.0" does not match the major version "v0" declared in ".*module.cue"; must be v0.N.N'
! exec cue mod publish v2.0.0
stderr 'publish version "v2.0.0" does not match the major version "v0" declared in ".*module.cue"; must be v0.N.N'

# Publishing with the right major version works.
exec cue mod publish v0.0.2

-- export.stdout --
{
    "out": "hello world"
}
-- cue.mod/module.cue --
module: "main.example"
language: version: "v0.9.0"
source: kind: "self"
-- cue.mod/module.cue.fixed --
module: "main.example"
language: {
	version: "v0.9.0"
}
source: {
	kind: "self"
}
-- cue.mod/module.cue.edited-with-v0 --
module: "main.example@v0"
language: {
	version: "v0.9.0"
}
source: {
	kind: "self"
}
-- main.cue --
package main

out: "hello world"
