use super::*;

#[test]
fn macos() {
  if cfg!(not(target_os = "macos")) {
    return;
  }
  let tempdir = tempdir();

  let path = tempdir.path().to_owned();

  Test::with_tempdir(tempdir)
    .write(
      "Library/Application Support/just/justfile",
      "
        @default:
          echo foo
      ",
    )
    .env("HOME", path.to_str().unwrap())
    .args(["--global-justfile"])
    .stdout("foo\n")
    .success();
}

#[test]
fn not_macos() {
  if cfg!(any(not(unix), target_os = "macos")) {
    return;
  }
  let tempdir = tempdir();

  let path = tempdir.path().to_owned();

  Test::with_tempdir(tempdir)
    .write(
      "just/justfile",
      "
        @default:
          echo foo
      ",
    )
    .env("XDG_CONFIG_HOME", path.to_str().unwrap())
    .args(["--global-justfile"])
    .stdout("foo\n")
    .success();
}

#[test]
fn unix() {
  if cfg!(not(unix)) {
    return;
  }
  let tempdir = tempdir();

  let path = tempdir.path().to_owned();

  let tempdir = Test::with_tempdir(tempdir)
    .write(
      "justfile",
      "
        @default:
          echo foo
      ",
    )
    .env("HOME", path.to_str().unwrap())
    .args(["--global-justfile"])
    .stdout("foo\n")
    .success()
    .tempdir;

  Test::with_tempdir(tempdir)
    .write(
      ".config/just/justfile",
      "
        @default:
          echo bar
      ",
    )
    .env("HOME", path.to_str().unwrap())
    .args(["--global-justfile"])
    .stdout("bar\n")
    .success();
}

#[test]
fn case_insensitive() {
  if cfg!(any(not(unix), target_os = "macos")) {
    return;
  }
  let tempdir = tempdir();

  let path = tempdir.path().to_owned();

  Test::with_tempdir(tempdir)
    .write(
      "just/JUSTFILE",
      "
        @default:
          echo foo
      ",
    )
    .env("XDG_CONFIG_HOME", path.to_str().unwrap())
    .args(["--global-justfile"])
    .stdout("foo\n")
    .success();
}

#[test]
fn forbid_search_path_prefix() {
  let tempdir = tempdir();

  let path = tempdir.path().to_owned();

  Test::with_tempdir(tempdir)
    .write(".config/just/justfile", "foo:\n  @echo global\n")
    .env("HOME", path.to_str().unwrap())
    .args(["--global-justfile", "sub/foo"])
    .stderr(
      "error: path-prefixed recipes may not be used with `--global-justfile`, \
      `--working-directory`, or `--justfile`\n",
    )
    .status(1);
}
