clean_test.dart 2.87 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:args/command_runner.dart';
8 9
import 'package:conductor/clean.dart';
import 'package:conductor/repository.dart';
10 11 12 13 14
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:platform/platform.dart';

import './common.dart';
15
import '../../../packages/flutter_tools/test/src/fake_process_manager.dart';
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

void main() {
  group('clean command', () {
    const String flutterRoot = '/flutter';
    const String checkoutsParentDirectory = '$flutterRoot/dev/tools/';

    MemoryFileSystem fileSystem;
    FakePlatform platform;
    TestStdio stdio;
    FakeProcessManager processManager;

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

    tearDown(() {
      // Ensure these don't get re-used between tests
      stdio = null;
      fileSystem = null;
      processManager = null;
      platform = null;
    });

    CommandRunner<void> createRunner({
      List<FakeCommand> commands,
      String operatingSystem,
    }) {
      operatingSystem ??= const LocalPlatform().operatingSystem;
      final String pathSeparator = operatingSystem == 'windows' ? r'\' : '/';

      processManager = FakeProcessManager.list(commands ?? <FakeCommand>[]);
      platform = FakePlatform(
        environment: <String, String>{'HOME': '/path/to/user/home'},
        pathSeparator: pathSeparator,
      );
      final Checkouts checkouts = Checkouts(
        fileSystem: fileSystem,
        parentDirectory: fileSystem.directory(checkoutsParentDirectory),
        platform: platform,
        processManager: processManager,
        stdio: stdio,
      );
      final CleanCommand command = CleanCommand(
        checkouts: checkouts,
      );
      return CommandRunner<void>('clean-test', '')..addCommand(command);
    }

    test('throws if no state file found', () async {
      final CommandRunner<void> runner = createRunner();
      const String stateFile = '/state-file.json';

      await expectLater(
        () async => runner.run(<String>[
          'clean',
          '--$kStateOption',
          stateFile,
          '--$kYesFlag',
        ]),
        throwsExceptionWith(
          'No persistent state file found at $stateFile',
        ),
      );
    });

    test('deletes state file', () async {
      final CommandRunner<void> runner = createRunner();
      final File stateFile = fileSystem.file('/state-file.json');
      stateFile.writeAsStringSync('{}');

      await runner.run(<String>[
        'clean',
        '--$kStateOption',
        stateFile.path,
        '--$kYesFlag',
      ]);

      expect(stateFile.existsSync(), false);
    });
  }, onPlatform: <String, dynamic>{
    'windows': const Skip('Flutter Conductor only supported on macos/linux'),
  });
}