test_test.dart 4.86 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io' hide Platform;

7 8 9
import 'package:file/file.dart' as fs;
import 'package:file/memory.dart';
import 'package:path/path.dart' as path;
10
import 'package:process/process.dart';
11 12 13 14

import '../test.dart';
import 'common.dart';

15 16 17 18 19
/// Fails a test if the exit code of `result` is not the expected value. This
/// is favored over `expect(result.exitCode, expectedExitCode)` because this
/// will include the process result's stdio in the failure message.
void expectExitCode(ProcessResult result, int expectedExitCode) {
  if (result.exitCode != expectedExitCode) {
20 21 22 23 24 25 26 27 28 29 30
    fail(
      'Process ${result.pid} exitted with the wrong exit code.\n'
      '\n'
      'EXPECTED: exit code $expectedExitCode\n'
      'ACTUAL: exit code ${result.exitCode}\n'
      '\n'
      'STDOUT:\n'
      '${result.stdout}\n'
      'STDERR:\n'
      '${result.stderr}'
    );
31 32 33
  }
}

34 35
void main() {
  group('verifyVersion()', () {
36
    late MemoryFileSystem fileSystem;
37 38 39 40 41

    setUp(() {
      fileSystem = MemoryFileSystem.test();
    });

42 43 44 45
    test('passes for valid version strings', () async {
      const List<String> valid_versions = <String>[
        '1.2.3',
        '12.34.56',
46 47 48
        '1.2.3.pre.1',
        '1.2.3-4.5.pre',
        '1.2.3-5.0.pre.12',
49
      ];
50
      for (final String version in valid_versions) {
51 52 53
        final File file = fileSystem.file('version');
        file.writeAsStringSync(version);

54 55 56 57 58
        expect(
          await verifyVersion(file),
          isNull,
          reason: '$version is valid but verifyVersionFile said it was bad',
        );
59 60 61 62 63 64 65
      }
    });

    test('fails for invalid version strings', () async {
      const List<String> invalid_versions = <String>[
        '1.2.3.4',
        '1.2.3.',
66 67
        '1.2.pre.1',
        '1.2.3-pre.1',
68 69
        '1.2.3-pre.1+hotfix.1',
        '  1.2.3',
70
        '1.2.3-hotfix.1',
71
      ];
72
      for (final String version in invalid_versions) {
73 74 75
        final File file = fileSystem.file('version');
        file.writeAsStringSync(version);

76 77 78 79 80
        expect(
          await verifyVersion(file),
          'The version logic generated an invalid version string: "$version".',
          reason: '$version is invalid but verifyVersionFile said it was fine',
        );
81 82 83
      }
    });
  });
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  group('flutter/plugins version', () {
    final MemoryFileSystem memoryFileSystem = MemoryFileSystem();
    final fs.File pluginsVersionFile = memoryFileSystem.file(path.join('bin','internal','flutter_plugins.version'));
    const String kSampleHash = '592b5b27431689336fa4c721a099eedf787aeb56';
    setUpAll(() {
      pluginsVersionFile.createSync(recursive: true);
    });

    test('commit hash', () async {
      pluginsVersionFile.writeAsStringSync(kSampleHash);
      final String actualHash = await getFlutterPluginsVersion(fileSystem: memoryFileSystem, pluginsVersionFile: pluginsVersionFile.path);
      expect(actualHash, kSampleHash);
    });

    test('commit hash with newlines', () async {
      pluginsVersionFile.writeAsStringSync('\n$kSampleHash\n');
      final String actualHash = await getFlutterPluginsVersion(fileSystem: memoryFileSystem, pluginsVersionFile: pluginsVersionFile.path);
      expect(actualHash, kSampleHash);
    });
  });
105 106 107 108

  group('test.dart script', () {
    const ProcessManager processManager = LocalProcessManager();

109 110 111 112
    Future<ProcessResult> runScript([
        Map<String, String>? environment,
        List<String> otherArgs = const <String>[],
    ]) async {
113
      final String dart = path.absolute(
114 115
        path.join('..', '..', 'bin', 'cache', 'dart-sdk', 'bin', 'dart'),
      );
116 117 118 119 120 121 122 123 124
      final ProcessResult scriptProcess = processManager.runSync(<String>[
        dart,
        'test.dart',
        ...otherArgs,
      ], environment: environment);
      return scriptProcess;
    }

    test('subshards tests correctly', () async {
125 126
      // When updating this test, try to pick shard numbers that ensure we're checking
      // that unequal test distributions don't miss tests.
127
      ProcessResult result = await runScript(
128
        <String, String>{'SHARD': kTestHarnessShardName, 'SUBSHARD': '1_3'},
129
      );
130
      expectExitCode(result, 0);
131
      expect(result.stdout, contains('Selecting subshard 1 of 3 (tests 1-3 of 8)'));
132 133

      result = await runScript(
134
        <String, String>{'SHARD': kTestHarnessShardName, 'SUBSHARD': '3_3'},
135
      );
136
      expectExitCode(result, 0);
137
      expect(result.stdout, contains('Selecting subshard 3 of 3 (tests 7-8 of 8)'));
138 139 140 141
    });

    test('exits with code 1 when SUBSHARD index greater than total', () async {
      final ProcessResult result = await runScript(
142
        <String, String>{'SHARD': kTestHarnessShardName, 'SUBSHARD': '100_99'},
143
      );
144
      expectExitCode(result, 1);
145 146 147
      expect(result.stdout, contains('Invalid subshard name'));
    });
  });
148
}