test_test.dart 4.64 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 20 21 22 23
/// 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) {
    fail('Failure due to exit code ${result.exitCode}\nSTDOUT:\n${result.stdout}\nSTDERR:\n${result.stderr}');
  }
}

24 25
void main() {
  group('verifyVersion()', () {
26
    late MemoryFileSystem fileSystem;
27 28 29 30 31

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

32 33 34 35
    test('passes for valid version strings', () async {
      const List<String> valid_versions = <String>[
        '1.2.3',
        '12.34.56',
36 37 38
        '1.2.3.pre.1',
        '1.2.3-4.5.pre',
        '1.2.3-5.0.pre.12',
39
      ];
40
      for (final String version in valid_versions) {
41 42 43
        final File file = fileSystem.file('version');
        file.writeAsStringSync(version);

44 45 46 47 48
        expect(
          await verifyVersion(file),
          isNull,
          reason: '$version is valid but verifyVersionFile said it was bad',
        );
49 50 51 52 53 54 55
      }
    });

    test('fails for invalid version strings', () async {
      const List<String> invalid_versions = <String>[
        '1.2.3.4',
        '1.2.3.',
56 57
        '1.2.pre.1',
        '1.2.3-pre.1',
58 59
        '1.2.3-pre.1+hotfix.1',
        '  1.2.3',
60
        '1.2.3-hotfix.1',
61
      ];
62
      for (final String version in invalid_versions) {
63 64 65
        final File file = fileSystem.file('version');
        file.writeAsStringSync(version);

66 67 68 69 70
        expect(
          await verifyVersion(file),
          'The version logic generated an invalid version string: "$version".',
          reason: '$version is invalid but verifyVersionFile said it was fine',
        );
71 72 73
      }
    });
  });
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  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);
    });
  });
95 96 97 98 99

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

    Future<ProcessResult> runScript(
100
        [Map<String, String>? environment, List<String> otherArgs = const <String>[]]) async {
101 102 103 104 105 106 107 108 109 110 111
      final String dart = path.absolute(
          path.join('..', '..', 'bin', 'cache', 'dart-sdk', 'bin', 'dart'));
      final ProcessResult scriptProcess = processManager.runSync(<String>[
        dart,
        'test.dart',
        ...otherArgs,
      ], environment: environment);
      return scriptProcess;
    }

    test('subshards tests correctly', () async {
112 113
      // When updating this test, try to pick shard numbers that ensure we're checking
      // that unequal test distributions don't miss tests.
114 115 116
      ProcessResult result = await runScript(
        <String, String>{'SHARD': 'smoke_tests', 'SUBSHARD': '1_3'},
      );
117
      expectExitCode(result, 0);
118
      expect(result.stdout, contains('Selecting subshard 1 of 3 (range 1-3 of 8)'));
119 120

      result = await runScript(
121
        <String, String>{'SHARD': 'smoke_tests', 'SUBSHARD': '3_3'},
122
      );
123
      expectExitCode(result, 0);
124
      expect(result.stdout, contains('Selecting subshard 3 of 3 (range 7-8 of 8)'));
125 126 127 128 129 130
    });

    test('exits with code 1 when SUBSHARD index greater than total', () async {
      final ProcessResult result = await runScript(
        <String, String>{'SHARD': 'smoke_tests', 'SUBSHARD': '100_99'},
      );
131
      expectExitCode(result, 1);
132 133 134
      expect(result.stdout, contains('Invalid subshard name'));
    });
  });
135
}