# Embedding is enabled by default, but it can still be disabled.
env CUE_EXPERIMENT=embed=0
! exec cue eval
cmp stderr out/noembed
env CUE_EXPERIMENT=

# Create test files for type=binary using strconv-unquote as txtar can only hold UTF8.
strconv-unquote '"\xef\xbb\xbfHello, 世界"'
cp stdout test-utf8-bom.binary
strconv-unquote "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7"
cp stdout test-nonutf8.binary

exec cue eval
cmp stdout out/eval

exec cue export --out cue
cmp stdout out/export

exec cue vet
cmp stdout out/vet

exec cue cmd test
cmp stdout out/cmd

-- cue.mod/module.cue --
module: "cue.example"
language: version: "v0.11.0"

-- test.cue --
@extern(embed)

package foo

a: _ @embed(file="test.json")

b: _ @embed(file="input.yaml")

c: _ @embed(file="test.json", type=text)

d: _ @embed(glob="y/*", type=yaml)

d: _ @embed(glob="x/*.yaml") // merge into the same map

f: _ @embed(file="openapi.json", type=openapi)

g: _ @embed(file="openapi.json") // test no auto mode!

h: _ @embed(file="test-utf8-bom.binary", type=binary)

i: _ @embed(file="test-utf8-bom.binary", type=text)

j: _ @embed(file="test-nonutf8.binary", type=binary)

special: {
	// These are all valid.
	underscoreFile: _ @embed(file="y/_test.json")
	dotFile:        _ @embed(file="y/.test.json")
	dotFileExplicit: _ @embed(glob="y/.*.json", type=yaml)
	dotdotFile:        _ @embed(file="..dotdot.json")
	underscoreDir:  _ @embed(file="_y/test.json")
	dotDir:         _ @embed(file=".y/test.json")
}

-- foo_tool.cue --
@extern(embed)

package foo

import (
    "strings"
    "tool/cli"
)

embedded_toolfile: _ @embed(file="input.yaml", type=text)

command: test: {
    regularFile: cli.Print & {
        text: "from foo.cue: \(strings.TrimSpace(c))"
    }
    toolFile: cli.Print & {
        $after: regularFile // for consistent output
        text: "from foo_tool.cue: \(strings.TrimSpace(embedded_toolfile))"
    }
}

-- test.json --
{ "x": 34 }
-- input.yaml --
a1: 2
-- ..dotdot.json --
{"dotdot": true}
-- y/test.json --
{ "x": 34 }
-- y/_test.json --
{ "z": 45 }
-- y/.test.json --
{ "z": 46 }
-- _y/test.json --
{ "z": 47 }
-- .y/test.json --
{ "z": 48 }
-- y/subdir/another.json --
{ "z": 49 }
-- _z/test.json --
-- x/input.yaml --
a1: 2
-- a/b/cue.mod/modules.cue --
module: "acme.com"
language: version: "v0.9.0"
-- a/b/foo.json --
{"a": 1, "b": 2}
-- openapi.json --
{
    "openapi": "3.0.0",
    "info": {
        "title":   "My OpenAPI",
        "version": "v1alpha1"
    },
    "paths": {},
    "components": {
        "schemas": {
            "Bar": {
                "type": "object",
                "required": [
                    "foo"
                ],
                "properties": {
                    "foo": {
                        "$ref": "#/components/schemas/Foo"
                    }
                }
            },
            "Foo": {
                "type": "object",
                "required": [
                    "a",
                    "b"
                ],
                "properties": {
                    "a": {
                        "type": "integer"
                    },
                    "b": {
                        "type": "integer",
                        "minimum": 0,
                        "exclusiveMaximum": 10
                    }
                }
            }
        }
    }
}
-- out/noembed --
no interpreter defined for "embed":
    ./test.cue:1:1
-- out/eval --
a: {
    x: 34
}
b: {
    a1: 2
}
c: """
    { "x": 34 }

    """
d: {
    "x/input.yaml": {
        a1: 2
    }
    "y/_test.json": {
        z: 45
    }
    "y/test.json": {
        x: 34
    }
}
f: {
    info: {
        title:   "My OpenAPI"
        version: "v1alpha1"
    }
    #Bar: {
        foo!: {
            a!: int
            b!: uint & <10
        }
    }
    #Foo: {
        a!: int
        b!: uint & <10
    }
}
g: {
    openapi: "3.0.0"
    info: {
        title:   "My OpenAPI"
        version: "v1alpha1"
    }
    paths: {}
    components: {
        schemas: {
            Bar: {
                type: "object"
                required: ["foo"]
                properties: {
                    foo: {
                        $ref: "#/components/schemas/Foo"
                    }
                }
            }
            Foo: {
                type: "object"
                required: ["a", "b"]
                properties: {
                    a: {
                        type: "integer"
                    }
                    b: {
                        type:             "integer"
                        minimum:          0
                        exclusiveMaximum: 10
                    }
                }
            }
        }
    }
}
h: '''
    \ufeffHello, 世界

    '''
i: """
    Hello, 世界

    """
j: '''
    \xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7

    '''
special: {
    underscoreFile: {
        z: 45
    }
    dotFile: {
        z: 46
    }
    dotFileExplicit: {
        "y/.test.json": {
            z: 46
        }
    }
    dotdotFile: {
        dotdot: true
    }
    underscoreDir: {
        z: 47
    }
    dotDir: {
        z: 48
    }
}
-- out/export --
a: x:  34
b: a1: 2
c: """
	{ "x": 34 }

	"""
d: {
	"x/input.yaml": a1: 2
	"y/_test.json": z:  45
	"y/test.json": x:   34
}
f: info: {
	title:   "My OpenAPI"
	version: "v1alpha1"
}
g: {
	openapi: "3.0.0"
	info: {
		title:   "My OpenAPI"
		version: "v1alpha1"
	}
	paths: {}
	components: schemas: {
		Bar: {
			type: "object"
			required: ["foo"]
			properties: foo: $ref: "#/components/schemas/Foo"
		}
		Foo: {
			type: "object"
			required: ["a", "b"]
			properties: {
				a: type: "integer"
				b: {
					type:             "integer"
					minimum:          0
					exclusiveMaximum: 10
				}
			}
		}
	}
}
h: '''
	\ufeffHello, 世界

	'''
i: """
	Hello, 世界

	"""
j: '''
	\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7

	'''
special: {
	// These are all valid.
	underscoreFile: z: 45
	dotFile: z:        46
	dotFileExplicit: "y/.test.json": z: 46
	dotdotFile: dotdot: true
	underscoreDir: z:   47
	dotDir: z:          48
}
-- out/vet --
-- out/cmd --
from foo.cue: { "x": 34 }
from foo_tool.cue: a1: 2
