2

I am porting old vm unittest files using the new test package. Some relies on input files in sub directories of my test folder. Before I was using Platform.script to find the location of such files. This works fine when using

$ dart test/my_test.dart

However using

$ pub run test

this is now pointing to a temp folder (tmp/dart_test_xxxx/runInIsolate.dart). I am unable to locate my test input files anymore. I cannot rely on the current path as I might run the test from a different working directory.

Is there a way to find the location of my_test.dart (or event the project root path), from which I could derive the locations of my files?

alextk
  • 5,713
  • 21
  • 34

2 Answers2

1

This is a current limitation of pub run.

What I currently do when I run into such requirements is to set an environment variable and read them from within the tests.

I have them set in my OS and set them from grinder on other systems before launching tests. This also works nice from WebStorm where launch configurations allow to specify environment variables.

This might be related http://dartbug.com/21020

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks Günter. I will wait for an eventual suggestion before accepting your answer. I already miss the old solo_test and DartEditor for debugging. Migration to WebStorm is not that easy; I hope it'll get better overtime. – alextk May 13 '15 at 13:35
  • Instead of solo_test you have the --name argument but we need WebStorm support and debugging support to be able to properly use it. – Günter Zöchbauer May 13 '15 at 13:57
0

I have the following workaround in the meantime. It is an ugly workaround that gets me the directory name of the current test script if I'm running it directly or with pub run test. It will definitely break if anything in the implementation changes but I needed this desperately...

library test_utils.test_script_dir;

import 'dart:io';
import 'package:path/path.dart';

// temp workaround using test package
String get testScriptDir {
  String scriptFilePath = Platform.script.toFilePath();
  print(scriptFilePath);
  if (scriptFilePath.endsWith("runInIsolate.dart")) {

    // Let's look for this line:
    // import "file:///path_to_my_test/test_test.dart" as test;

    String importLineBegin = 'import "file://';
    String importLineEnd = '" as test;';
    int importLineBeginLength = importLineBegin.length;

    String scriptContent = new File.fromUri(Platform.script).readAsStringSync();

    int beginIndex = scriptContent.indexOf(importLineBegin);
    if (beginIndex > -1) {
      int endIndex = scriptContent.indexOf(importLineEnd, beginIndex + importLineBeginLength);
      if (endIndex > -1) {
        scriptFilePath = scriptContent.substring(beginIndex + importLineBegin.length, endIndex);
      }
    }
  }
  return dirname(scriptFilePath);
}
alextk
  • 5,713
  • 21
  • 34