[!windows] chmod 755 bin/chezmoi-output-test
[!windows] chmod 755 bin/generate-color-formats
[!windows] chmod 755 bin/ioreg
[windows] unix2dos bin/chezmoi-output-test.cmd

# test ioreg template function
[darwin] chezmoi execute-template '{{ index ioreg "IOKitBuildVersion" }}'
[darwin] stdout 'Darwin Kernel Version'

# test include template function with absolute path
#
# this test is disabled on Windows because the backslashes in Windows paths are
# interpreted as a escape characters in the string constant, which breaks the
# test
#
# FIXME fix this test on Windows
[!windows] exec echo {{ "$HOME/.include" }}
[!windows] stdin stdout
[!windows] chezmoi execute-template
[!windows] cmpenv stdout golden/include-abspath

# test include template function with relative paths
chezmoi execute-template '{{ include ".include" }}'
cmp stdout golden/include-relpath

# test joinPath template function
chezmoi execute-template '{{ joinPath "a" "b" }}'
stdout a${/}b

# test lookPath template function to find in PATH
chezmoi execute-template '{{ lookPath "go" }}'
stdout go$exe

# test lookPath template function to check if file exists
chezmoi execute-template '{{ lookPath "/non-existing-file" }}'
! stdout .

# test mozillaInstallHash template function
chezmoi execute-template '{{ mozillaInstallHash "/Applications/Firefox.app/Contents/MacOS" }}'
stdout 2656FF1E876E9973

# test the output and fromJson template functions
[!windows] chezmoi execute-template '{{ $red := output "generate-color-formats" "#ff0000" | fromJson }}{{ $red.rgb.r }}'
[!windows] stdout '^255$'

# test that the output function returns an error if the command fails
[!windows] ! chezmoi execute-template '{{ output "false" }}'
[!(windows||illumos)] stderr 'error calling output: exit status 1'
[illumos] stderr 'error calling output: exit status 255'

# test stat template function
chezmoi execute-template '{{ (stat ".").isDir }}'
stdout true

# test that the output template function returns a command's output
chezmoi execute-template '{{ output "chezmoi-output-test" "arg" | trim }}'
stdout arg

# test that the output template function fails if the command fails
! chezmoi execute-template '{{ output "false" }}'

# test fromYaml
chezmoi execute-template '{{ (fromYaml "key1: value1\nkey2: value2").key2 }}'
stdout '^value2$'

# test toYaml
chezmoi execute-template '{{ dict "key" "value" | toYaml }}'
stdout '^key: value$'

# test writeToStdout
chezmoi execute-template --init '{{ writeToStdout "string" }}'
stdout string

-- bin/chezmoi-output-test --
#!/bin/sh

echo "$*"
-- bin/chezmoi-output-test.cmd --
@echo off
setlocal
set out=%*
set out=%out:\=%
echo %out%
endlocal
-- bin/generate-color-formats --
#!/bin/sh

case "$1" in
"#ff0000")
    cat <<EOF
{
  "hex": {
    "rgb": "ff0000",
    "bgr": "0000ff",
    "r": "ff",
    "g": "00",
    "b": "00"
  },
  "rgb": {
    "r": 255,
    "g": 0,
    "b": 0
  },
  "dec": {
    "r": 1.000,
    "g": 0.000,
    "b": 0.000
  }
}
EOF
    ;;
*)
    echo "Usage: $0 <hex-color>"
    ;;
esac
-- bin/ioreg --
#!/bin/sh

echo '<?xml version="1.0" encoding="UTF-8"?>'
echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
echo '<plist version="1.0">'
echo '<dict>'
echo '        <key>IOKitBuildVersion</key>'
echo '        <string>Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101</string>'
echo '</dict>'
echo '</plist>'
-- golden/expected --
255
-- golden/include-abspath --
$HOME/.include
-- golden/include-relpath --
# contents of .local/share/chezmoi/.include
-- home/user/.include --
# contents of $HOME/.include
-- home/user/.local/share/chezmoi/.include --
# contents of .local/share/chezmoi/.include
