public static String findTestUnitScript()

in rake-runner-agent/src/jetbrains/buildServer/agent/rakerunner/utils/TestUnitUtil.java [72:172]


  public static String findTestUnitScript(@NotNull final RubySdk sdk,
                                          @NotNull final String scriptPath,
                                          @NotNull final ModifiableRunnerContext context)
  throws RakeTasksBuildService.MyBuildFailureException, RunBuildException {

    // At first let's try to find script in "test-unit" gem
    // then in sdk load path

    final String forcedTestUnitGemVersion = RubySDKUtil.getForcedGemVersion(TEST_UNIT_GEM_VERSION_PROPERTY, context.getBuildParameters());

    // if option is "built-in" let's use built-in Test::Unit in Ruby 1.8.x sdk
    // else use custom gem version
    final boolean forceUseBuiltInTestUnit = RakeRunnerConstants.TEST_UNIT_USE_BUILTIN_VERSION_PARAM.equals(forcedTestUnitGemVersion);
    if (!forceUseBuiltInTestUnit) {

      // use bundler gems root if it is defined! (i.e. we use bundle exec emulation with custom gem paths)
      final String bundlerGemRoot = BundlerUtil.determineGemsRootsAccordingToBundlerSettings(sdk, context);
      final String[] gemPaths = bundlerGemRoot == null ? sdk.getGemPaths() : new String[]{bundlerGemRoot};

      // If user overrides bundler.path sys var or uses project custom bundle..
      // we need to look for test-unit gems in "frozen" paths
      final Pair<String, String> gemInfo;
      if (forcedTestUnitGemVersion != null) {
        gemInfo = RubySDKUtil.findGemRootFolderAndVersion(TEST_UNIT_GEM_NAME,
                                                          gemPaths,
                                                          forcedTestUnitGemVersion);
      } else {
        List<Pair<String, String>> gems = RubySDKUtil.findGemsByName(TEST_UNIT_GEM_NAME, gemPaths);
        final String version = sdk.getVersion();
        final String gv = version != null ? version + ".0" : "2.0.0.0";
        gems = CollectionsUtil.filterCollection(gems, new Filter<Pair<String, String>>() {
          public boolean accept(@NotNull final Pair<String, String> data) {
            // Ruby-2.0.0 and newer with test-unit compatibility layer using minitest
            return !data.second.equals(gv);
          }
        });
        gemInfo = gems.isEmpty() ? null : Collections.max(gems, new RubySDKUtil.GemInfoPairComparator());
      }


      if (gemInfo != null) {
        final String path = gemInfo.first;
        final String version = gemInfo.second;
        final String fullScriptPath = path + File.separatorChar + "lib" + File.separatorChar + scriptPath;
        if (FileUtil2.checkIfExists(fullScriptPath)) {
          return fullScriptPath;
        } else {

          // Error: Script wasn't found in test-unit gem
          final String msg = "Rake runner isn't compatible with your'" + TEST_UNIT_GEM_NAME + "-" + version
                             + "'(" + path + ") gem. Please submit a feature request.";
          throw new RakeTasksBuildService.MyBuildFailureException(msg);
        }

      } else {
        // test-unit gem not found
        if (forcedTestUnitGemVersion != null) {
          // not "built-in", but something specified
          final String msg = "test-unit gem with version '"
                             + forcedTestUnitGemVersion
                             + "' wasn't found in Gem paths of Ruby SDK with interpreter: '"
                             + sdk.getName()
                             + "'.\n"
                             + "Gem paths:\n"
                             + (bundlerGemRoot == null ? sdk.getGemPathsFetchLog().getStdout() : bundlerGemRoot);
          throw new RakeTasksBuildService.MyBuildFailureException(msg);
        }
      }
    }

    // find test-unit in load path
    final String fullScriptPath = findInSdkRoots(sdk, scriptPath);
    if (fullScriptPath != null) {
      return fullScriptPath;
    }

    // If stderr isn't empty / JAVA_HOME error
    final ExecResult gemPathsLog = sdk.getGemPathsFetchLog();
    RubySDKUtil.failIfWithErrors(gemPathsLog);
    final ExecResult loadPathsLog = sdk.getLoadPathsFetchLog();
    RubySDKUtil.failIfWithErrors(loadPathsLog);


    // General error message
    final StringBuilder msg = new StringBuilder();
    if (forceUseBuiltInTestUnit) {
      msg.append("You asked TC to use built-in Test::Unit test framework, but file '");
    } else {
      msg.append("File '");
    }
    msg.append(scriptPath).append("' wasn't found in Gem paths and in $LOAD_PATH of " +
                                  "Ruby SDK with interpreter: '").append(sdk.getName()).append("'.\n");
    if (sdk.isRuby19()) {
      msg.append("Rake runner detected that your are using Ruby 1.9. " +
                 "So please install 'test-unit' gem because simplified Test::Unit framework, " +
                 "which is bundled in Ruby 1.9, doesn't support pluggable test reporters.\n");
    }
    msg.append("\nGem paths:\n").append(gemPathsLog.getStdout()).append("\n\n");
    msg.append("$LOAD_PATH:\n").append(loadPathsLog.getStdout());
    throw new RakeTasksBuildService.MyBuildFailureException(msg.toString());
  }