# An empty argument is not a valid package pattern nor filename.
! exec cue export ''
stderr 'empty file name'

# Unknown filetype prefix.
! exec cue export cue+date: somefile.cue
stderr 'unknown filetype date'

# Trying to parse as the wrong filetype.
! exec cue export json: somefile.cue
stderr 'invalid JSON for file'

# Trying to parse as the explicitly right filetype.
exec cue export cue: somefile.cue
cmp stdout export.golden

# Filetype detection works with absolute or relative paths, including dot prefixes.
exec cue export somefile.cue
cmp stdout export.golden
exec cue export ./somefile.cue
cmp stdout export.golden
exec cue export ${WORK}${/}somefile.cue
cmp stdout export.golden

# A package can be loaded as a pattern or as a directory, with or without trailing slashes.
exec cue export ./somepkg
cmp stdout export.golden
exec cue export ./somepkg/
cmp stdout export.golden
exec cue export ${WORK}${/}somepkg
cmp stdout export.golden
exec cue export ${WORK}${/}somepkg${/}
cmp stdout export.golden

# Now do some of the same filename tests, but for a file without any extension.
cp somefile.cue somefile

! exec cue export json: somefile
stderr 'invalid JSON for file'

exec cue export cue: somefile
cmp stdout export.golden
exec cue export cue: ./somefile
cmp stdout export.golden
exec cue export cue: ${WORK}${/}somefile
cmp stdout export.golden

# Without an extension nor a filetype, "somefile" on its own looks like a std package.
! exec cue export somefile
stderr 'standard library import path "somefile" cannot be imported as a CUE package'

! exec cue export ./somefile
stderr '\./somefile is a file and not a package directory'
! exec cue export ${WORK}${/}somefile
stderr '\./somefile is a file and not a package directory'

# A package path pointing outside the current module is rejected,
# whether given as a relative or absolute path.
cd nested
! exec cue export ./../somepkg
stderr 'cannot determine import path for "\./\.\./somepkg" \(dir outside of root\)'
! exec cue export ${WORK}${/}somepkg
stderr 'cannot determine import path for "\./\.\./somepkg" \(dir outside of root\)'
cd ${WORK}

-- cue.mod/module.cue --
module: "mod.test/test"
language: version: "v0.9.0"
-- somefile.cue --
a: 5
-- somepkg/somefile.cue --
package somepkg

a: 5
-- nested/cue.mod/module.cue --
module: "mod.test/nested"
language: version: "v0.9.0"
-- nested/x.cue --
package nested

a: 1
-- export.golden --
{
    "a": 5
}
