clean_test.dart 2.8 KB
Newer Older
1 2 3 4 5
// 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.

import 'package:args/command_runner.dart';
6 7
import 'package:conductor_core/src/clean.dart';
import 'package:conductor_core/src/repository.dart';
8 9 10 11 12 13 14 15 16 17
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:platform/platform.dart';

import './common.dart';

void main() {
  group('clean command', () {
    const String flutterRoot = '/flutter';
    const String checkoutsParentDirectory = '$flutterRoot/dev/tools/';
Alex's avatar
Alex committed
18
    const String stateFilePath = '/state-file.json';
19

20 21 22 23 24
    late MemoryFileSystem fileSystem;
    late FakePlatform platform;
    late TestStdio stdio;
    late FakeProcessManager processManager;
    late CommandRunner<void> runner;
25 26 27 28

    setUp(() {
      stdio = TestStdio();
      fileSystem = MemoryFileSystem.test();
29
      final String operatingSystem = const LocalPlatform().operatingSystem;
30 31
      final String pathSeparator = operatingSystem == 'windows' ? r'\' : '/';

32
      processManager = FakeProcessManager.empty();
33 34 35 36 37 38 39 40 41 42 43 44 45 46
      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,
      );
47 48
      runner = CommandRunner<void>('clean-test', '')..addCommand(command);
    });
49 50 51 52 53 54

    test('throws if no state file found', () async {
      await expectLater(
        () async => runner.run(<String>[
          'clean',
          '--$kStateOption',
Alex's avatar
Alex committed
55
          stateFilePath,
56 57 58
          '--$kYesFlag',
        ]),
        throwsExceptionWith(
Alex's avatar
Alex committed
59
          'No persistent state file found at $stateFilePath',
60 61 62 63
        ),
      );
    });

Alex's avatar
Alex committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    test('deletes an empty state file', () async {
      final File stateFile = fileSystem.file(stateFilePath);
      stateFile.writeAsStringSync('');

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

      expect(stateFile.existsSync(), false);
    });

    test('deletes a state file with content', () async {
      final File stateFile = fileSystem.file(stateFilePath);
      stateFile.writeAsStringSync('{status: pending}');
81 82 83 84 85 86 87 88 89 90 91 92 93 94

      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'),
  });
}