# Check that we can use the cue mod publish command to publish to a registry.
memregistry MEMREGISTRY
env ORIG_CUE_REGISTRY=$CUE_REGISTRY
env CUE_REGISTRY=example.com=$MEMREGISTRY+insecure,$CUE_REGISTRY
cd example
exec cue mod publish v0.0.1
stdout '^published example.com@v0.0.1 to [^ ]+/example.com:v0.0.1$'
cd ../main
exec cue eval .
cmp stdout ../expect-eval-stdout

# Sanity check that the module isn't present in the fallback registry.
env CUE_REGISTRY=$ORIG_CUE_REGISTRY
env CUE_CACHE_DIR=$WORK/.tmp/different-cache
! exec cue eval
stderr 'cannot fetch example.com@v0.0.1: module example.com@v0.0.1: module not found'

-- expect-eval-stdout --
"bar.com@v0":           "v0.5.0"
"baz.org@v0":           "v0.10.1"
"foo.com/bar/hello@v0": "v0.2.3"
main:                   "main"
"example.com@v0":       "v0.0.1"
-- main/cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.9.0-alpha.0"

source: kind: "self"

deps: {
	"bar.com@v0": v: "v0.5.0"
	"baz.org@v0": v: "v0.10.1"
	"example.com@v0": v: "v0.0.1"
	"foo.com/bar/hello@v0": {
		v:       "v0.2.3"
		default: true
	}
}

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

main

-- example/cue.mod/module.cue --
module: "example.com@v0"
language: version: "v0.9.0-alpha.0"
source: kind: "self"
deps: {
	"bar.com@v0": v: "v0.5.0"
	"baz.org@v0": v: "v0.10.1"
	"foo.com/bar/hello@v0": v: "v0.2.3"
}

-- example/top.cue --
package main

// Note: import without a major version takes
// the major version from the module.cue file.
import a "foo.com/bar/hello"
a
main: "main"
"example.com@v0": "v0.0.1"

-- _registry/foo.com_bar_hello_v0.2.3/cue.mod/module.cue --
module: "foo.com/bar/hello@v0"
language: version: "v0.8.0"
deps: {
	"bar.com@v0": v: "v0.0.2"
	"baz.org@v0": v: "v0.10.1"
}

-- _registry/foo.com_bar_hello_v0.2.3/x.cue --
package hello
import (
	a "bar.com/bar@v0"
	b "baz.org@v0:baz"
)
"foo.com/bar/hello@v0": "v0.2.3"
a
b


-- _registry/bar.com_v0.0.2/cue.mod/module.cue --
module: "bar.com@v0"
language: version: "v0.8.0"
deps: "baz.org@v0": v: "v0.0.2"

-- _registry/bar.com_v0.0.2/bar/x.cue --
package bar
import a "baz.org@v0:baz"
"bar.com@v0": "v0.0.2"
a


-- _registry/bar.com_v0.5.0/cue.mod/module.cue --
module: "bar.com@v0"
language: version: "v0.8.0"
deps: "baz.org@v0": v: "v0.5.0"

-- _registry/bar.com_v0.5.0/bar/x.cue --
package bar
import a "baz.org@v0:baz"
"bar.com@v0": "v0.5.0"
a


-- _registry/baz.org_v0.0.2/cue.mod/module.cue --
module: "baz.org@v0"
language: version: "v0.8.0"

-- _registry/baz.org_v0.0.2/baz.cue --
package baz
"baz.org@v0": "v0.0.2"


-- _registry/baz.org_v0.1.2/cue.mod/module.cue --
module: "baz.org@v0"
language: version: "v0.8.0"

-- _registry/baz.org_v0.1.2/baz.cue --
package baz
"baz.org@v0": "v0.1.2"


-- _registry/baz.org_v0.5.0/cue.mod/module.cue --
module: "baz.org@v0"
language: version: "v0.8.0"

-- _registry/baz.org_v0.5.0/baz.cue --
package baz
"baz.org@v0": "v0.5.0"


-- _registry/baz.org_v0.10.1/cue.mod/module.cue --
module: "baz.org@v0"
language: version: "v0.8.0"

-- _registry/baz.org_v0.10.1/baz.cue --
package baz
"baz.org@v0": "v0.10.1"
