<commit_before>// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library analyzer_cli.test.driver;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/error/error.dart';
import 'package:analyzer/source/analysis_options_provider.dart';
import 'package:analyzer/source/error_processor.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/services/lint.dart';
import 'package:analyzer_cli/src/driver.dart';
import 'package:analyzer_cli/src/options.dart';
import 'package:cli_util/cli_util.dart' show getSdkDir;
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:yaml/src/yaml_node.dart';

import 'utils.dart';

main() {
  StringSink savedOutSink, savedErrorSink;
  int savedExitCode;
  ExitHandler savedExitHandler;

  /// Base setup.
  _setUp() {
    savedOutSink = outSink;
    savedErrorSink = errorSink;
    savedExitHandler = exitHandler;
    savedExitCode = exitCode;
    exitHandler = (code) => exitCode = code;
    outSink = new StringBuffer();
    errorSink = new StringBuffer();
  }

  /// Base teardown.
  _tearDown() {
    outSink = savedOutSink;
    errorSink = savedErrorSink;
    exitCode = savedExitCode;
    exitHandler = savedExitHandler;
  }

  setUp(() => _setUp());

  tearDown(() => _tearDown());

  group('Driver', () {
    group('options', () {
      test('todos', () async {
        await drive('data/file_with_todo.dart');
        expect(outSink.toString().contains('[info]'), isFalse);
      });
    });

    group('exit codes', () {
      test('fatal hints', () async {
        await drive('data/file_with_hint.dart', args: ['--fatal-hints']);
        expect(exitCode, 3);
      });

      test('not fatal hints', () async {
        await drive('data/file_with_hint.dart');
        expect(exitCode, 0);
      });

      test('fatal errors', () async {
        await drive('data/file_with_error.dart');
        expect(exitCode, 3);
      });

      test('not fatal warnings', () async {
        await drive('data/file_with_warning.dart');
        expect(exitCode, 0);
      });

      test('fatal warnings', () async {
        await drive('data/file_with_warning.dart', args: ['--fatal-warnings']);
        expect(exitCode, 3);
      });

      test('missing options file', () async {
        await drive('data/test_file.dart', options: 'data/NO_OPTIONS_HERE');
        expect(exitCode, 3);
      });

      test('missing dart file', () async {
        await drive('data/NO_DART_FILE_HERE.dart');
        expect(exitCode, 3);
      });

      test('part file', () async {
        await drive('data/library_and_parts/part2.dart');
        expect(exitCode, 3);
      });

      test('non-dangling part file', () async {
        Driver driver = new Driver();
        await driver.start([
          path.join(testDirectory, 'data/library_and_parts/lib.dart'),
          path.join(testDirectory, 'data/library_and_parts/part1.dart')
        ]);
        expect(exitCode, 0);
      });

      test('extra part file', () async {
        Driver driver = new Driver();
        await driver.start([
          path.join(testDirectory, 'data/library_and_parts/lib.dart'),
          path.join(testDirectory, 'data/library_and_parts/part1.dart'),
          path.join(testDirectory, 'data/library_and_parts/part2.dart')
        ]);
        expect(exitCode, 3);
      });

      test('bazel workspace relative path', () async {
        // Copy to temp dir so that existing analysis options
        // in the test directory hierarchy do not interfere
        await withTempDirAsync((String tempDirPath) async {
          String dartSdkPath = path.absolute(getSdkDir(<String>[]).path);
          print('>>> dartSdkPath: $dartSdkPath');
          await recursiveCopy(
              new Directory(path.join(testDirectory, 'data', 'bazel')),
              tempDirPath);
          Directory origWorkingDir = Directory.current;
          try {
            Directory.current = path.join(tempDirPath, 'proj');
            Driver driver = new Driver();
            try {
              await driver.start([
                path.join('lib', 'file.dart'),
                '--dart-sdk',
                dartSdkPath,
              ]);
            } catch (e) {
              print('>>>>>\n${errorSink.toString()}\n<<<<<');
              rethrow;
            }
            expect(errorSink.toString(), isEmpty);
            expect(outSink.toString(), contains('No issues found'));
            expect(exitCode, 0);
          } finally {
            Directory.current = origWorkingDir;
          }
        });
      });
    });

    group('linter', () {
      void createTests(String designator, String optionsFileName) {
        group('lints in options - $designator', () {
          // Shared lint command.
          Future<Null> runLinter() async {
            return await drive('data/linter_project/test_file.dart',
                options: 'data/linter_project/$optionsFileName',
                args: ['--lints']);
          }

          test('gets analysis options', () async {<commit_msg>cleanup bazel workspace relative path test<commit_after>// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library analyzer_cli.test.driver;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/error/error.dart';
import 'package:analyzer/source/analysis_options_provider.dart';
import 'package:analyzer/source/error_processor.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/services/lint.dart';
import 'package:analyzer_cli/src/driver.dart';
import 'package:analyzer_cli/src/options.dart';
import 'package:cli_util/cli_util.dart' show getSdkDir;
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:yaml/src/yaml_node.dart';

import 'utils.dart';

main() {
  StringSink savedOutSink, savedErrorSink;
  int savedExitCode;
  ExitHandler savedExitHandler;

  /// Base setup.
  _setUp() {
    savedOutSink = outSink;
    savedErrorSink = errorSink;
    savedExitHandler = exitHandler;
    savedExitCode = exitCode;
    exitHandler = (code) => exitCode = code;
    outSink = new StringBuffer();
    errorSink = new StringBuffer();
  }

  /// Base teardown.
  _tearDown() {
    outSink = savedOutSink;
    errorSink = savedErrorSink;
    exitCode = savedExitCode;
    exitHandler = savedExitHandler;
  }

  setUp(() => _setUp());

  tearDown(() => _tearDown());

  group('Driver', () {
    group('options', () {
      test('todos', () async {
        await drive('data/file_with_todo.dart');
        expect(outSink.toString().contains('[info]'), isFalse);
      });
    });

    group('exit codes', () {
      test('fatal hints', () async {
        await drive('data/file_with_hint.dart', args: ['--fatal-hints']);
        expect(exitCode, 3);
      });

      test('not fatal hints', () async {
        await drive('data/file_with_hint.dart');
        expect(exitCode, 0);
      });

      test('fatal errors', () async {
        await drive('data/file_with_error.dart');
        expect(exitCode, 3);
      });

      test('not fatal warnings', () async {
        await drive('data/file_with_warning.dart');
        expect(exitCode, 0);
      });

      test('fatal warnings', () async {
        await drive('data/file_with_warning.dart', args: ['--fatal-warnings']);
        expect(exitCode, 3);
      });

      test('missing options file', () async {
        await drive('data/test_file.dart', options: 'data/NO_OPTIONS_HERE');
        expect(exitCode, 3);
      });

      test('missing dart file', () async {
        await drive('data/NO_DART_FILE_HERE.dart');
        expect(exitCode, 3);
      });

      test('part file', () async {
        await drive('data/library_and_parts/part2.dart');
        expect(exitCode, 3);
      });

      test('non-dangling part file', () async {
        Driver driver = new Driver();
        await driver.start([
          path.join(testDirectory, 'data/library_and_parts/lib.dart'),
          path.join(testDirectory, 'data/library_and_parts/part1.dart')
        ]);
        expect(exitCode, 0);
      });

      test('extra part file', () async {
        Driver driver = new Driver();
        await driver.start([
          path.join(testDirectory, 'data/library_and_parts/lib.dart'),
          path.join(testDirectory, 'data/library_and_parts/part1.dart'),
          path.join(testDirectory, 'data/library_and_parts/part2.dart')
        ]);
        expect(exitCode, 3);
      });

      test('bazel workspace relative path', () async {
        // Copy to temp dir so that existing analysis options
        // in the test directory hierarchy do not interfere
        await withTempDirAsync((String tempDirPath) async {
          String dartSdkPath = path.absolute(getSdkDir(<String>[]).path);
          await recursiveCopy(
              new Directory(path.join(testDirectory, 'data', 'bazel')),
              tempDirPath);
          Directory origWorkingDir = Directory.current;
          try {
            Directory.current = path.join(tempDirPath, 'proj');
            Driver driver = new Driver();
            try {
              await driver.start([
                path.join('lib', 'file.dart'),
                '--dart-sdk',
                dartSdkPath,
              ]);
            } catch (e) {
              print('=== debug info ===');
              print('dartSdkPath: $dartSdkPath');
              print('stderr:\n${errorSink.toString()}');
              rethrow;
            }
            expect(errorSink.toString(), isEmpty);
            expect(outSink.toString(), contains('No issues found'));
            expect(exitCode, 0);
          } finally {
            Directory.current = origWorkingDir;
          }
        });
      });
    });

    group('linter', () {
      void createTests(String designator, String optionsFileName) {
        group('lints in options - $designator', () {
          // Shared lint command.
          Future<Null> runLinter() async {
            return await drive('data/linter_project/test_file.dart',
                options: 'data/linter_project/$optionsFileName',
                args: ['--lints']);
          }

          test('gets analysis options', () async {
{'fraction_non_alphanumeric': 0.14079791686041104, 'fraction_numerical': 0.003533897516972008, 'mean_word_length': 2.4892926670992863, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1352489', 'n_tokens_mistral': 3251, 'n_tokens_neox': 3184, 'n_words': 663}