#!/usr/bin/env bats

load ../utils/_

setup() {
  standard_setup
  
  use_test_specific_cache_root
}

teardown() {
  dfx_stop

  standard_teardown
}

@test "dfx cache show does not install the dfx version into the cache" {
  test -z "$(ls -A "$DFX_CACHE_ROOT")"

  assert_command dfx cache show

  # does not populate the cache with this version
  test ! -e "$(dfx cache show)"

  # it does create the empty versions directory though
  test -d "$DFX_CACHE_ROOT/.cache/dfinity/versions"
  test -z "$(ls -A "$DFX_CACHE_ROOT/.cache/dfinity/versions"``)"
}

@test "non-forced install populates an empty cache" {
  test ! -e "$(dfx cache show)"/dfx

  dfx_new

  test -f "$(dfx cache show)"/dfx
}

@test "forced install populates an empty cache" {
  test ! -e "$(dfx cache show)"/dfx

  assert_command dfx cache install

  test -f "$(dfx cache show)"/dfx
}

@test "forced install over an install succeeds" {
  assert_command dfx cache install
  test -f "$(dfx cache show)"/dfx

  assert_command dfx cache install
}

@test "Motoko base library files are not executable" {
  assert_command dfx cache install
  for file in "$(dfx cache show)"/base/*.mo; do
    assert_command_fail test -x "$file"
    assert_command_fail "$file"
    assert_contains "Permission denied"
  done
}

@test "Motoko core package files are not executable" {
  assert_command dfx cache install
  for file in "$(dfx cache show)"/core/*.mo; do
    assert_command_fail test -x "$file"
    assert_command_fail "$file"
    assert_contains "Permission denied"
  done
}


@test "forced install overwrites a cached version" {
  assert_command dfx cache install
  test -f "$(dfx cache show)"/dfx

  # add something extra to the cache
  echo "garbage" >"$(dfx cache show)"/garbage
  test -f "$(dfx cache show)"/garbage

  assert_command dfx cache install

  # dfx cache install should have removed it
  test ! -e "$(dfx cache show)"/garbage

  # and also installed the cache itself
  test -f "$(dfx cache show)"/dfx
}
