### ----------------------------------------------------
### -- App::RaCoCo
### -- Licenses: Artistic-2.0
### -- Authors: Mikhail Khorkov
### -- File: t/lib/TestResources.rakumod
### ----------------------------------------------------
unit module TestResources;

use App::Racoco::ModuleNames;
use App::Racoco::Sha;
use TmpDir;
use Fixture;

our sub test-directory($up-calls = 3) {
  't-resources/'.IO
		.add((callframe($up-calls).file.IO.extension: '').basename)
		.absolute.IO
}

our sub subtest-directory($subtest, $up-calls = 3) {
  test-directory($up-calls).add($subtest)
}

our sub exam-directory() {
  't-resources/'.IO.add('exam').absolute.IO;
}

our sub prepare($subtest --> IO::Path) {
  my $exam-directory = exam-directory();
  TmpDir::rm_dir($exam-directory);
  TmpDir::create-dir($exam-directory);
  copy-content(from => subtest-directory($subtest, 4), to => $exam-directory);
}

sub copy-content(:$from, :$to is copy) {
  $to = make-dir($to);
  for eager $from.dir() -> $ls-from {
    my $ls-to = $to.add($ls-from.basename);
    if $ls-from.d {
      copy-content(from => $ls-from, to => $ls-to);
    } else {
      copy-file(from => $ls-from, to => $ls-to);
    }
  }
}

sub make-dir($create is copy) {
  return $create if $create ~~ :d & :e;
  $create = fix-name($create);
  $create.mkdir;
  $create
}

sub copy-file(:$from, :$to is copy) {
  $to = fix-name($to);
  $from.copy($to);
}

sub fix-name(IO::Path $path --> IO::Path) {
  my $basename = $path.basename;
  if $basename.starts-with('_') {
    return $path.parent.add('.' ~ $basename.substr(1));
  } elsif $basename eq 'current_compiler_id' {
    return $path.parent.add(Fixture::compiler-id());
  } elsif $basename.starts-with('precomp_') {
    my ($lib, $module-name) = $basename.split('_')[1, 2];
    $module-name .= subst('::', '/', :g);
    $lib = exam-directory().add($lib);
    my $precomp-path = file-precomp-path(:$lib, path => $module-name);
    try $path.parent.add($precomp-path.parent).mkdir;
    return $path.parent.add($precomp-path);
  } elsif $basename eq 'root-id' {
    my $root-hash-name =
    	App::Racoco::Sha::create.uc(exam-directory().add('root'));
    return $path.parent.add($root-hash-name);
  }
  return $path;
}


