shell_completion_test.dart 4.17 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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 8 9 10 11 12
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/shell_completion.dart';
13
import 'package:flutter_tools/src/globals.dart' as globals;
14

15
import '../../src/context.dart';
16
import '../../src/fakes.dart';
17
import '../../src/test_flutter_command_runner.dart';
18 19 20

void main() {
  group('shell_completion', () {
21
    FakeStdio fakeStdio;
22 23 24

    setUp(() {
      Cache.disableLocking();
25
      fakeStdio = FakeStdio()..stdout.terminalColumns = 80;
26 27 28
    });

    testUsingContext('generates bash initialization script to stdout', () async {
29
      final ShellCompletionCommand command = ShellCompletionCommand();
30
      await createTestCommandRunner(command).run(<String>['bash-completion']);
31 32
      expect(fakeStdio.writtenToStdout.length, equals(1));
      expect(fakeStdio.writtenToStdout.first, contains('__flutter_completion'));
33
    }, overrides: <Type, Generator>{
34 35
      FileSystem: () => MemoryFileSystem.test(),
      ProcessManager: () => FakeProcessManager.any(),
36
      Stdio: () => fakeStdio,
37 38 39
    });

    testUsingContext('generates bash initialization script to stdout with arg', () async {
40
      final ShellCompletionCommand command = ShellCompletionCommand();
41
      await createTestCommandRunner(command).run(<String>['bash-completion', '-']);
42 43
      expect(fakeStdio.writtenToStdout.length, equals(1));
      expect(fakeStdio.writtenToStdout.first, contains('__flutter_completion'));
44
    }, overrides: <Type, Generator>{
45 46
      FileSystem: () => MemoryFileSystem.test(),
      ProcessManager: () => FakeProcessManager.any(),
47
      Stdio: () => fakeStdio,
48 49 50
    });

    testUsingContext('generates bash initialization script to output file', () async {
51
      final ShellCompletionCommand command = ShellCompletionCommand();
52 53 54 55
      const String outputFile = 'bash-setup.sh';
      await createTestCommandRunner(command).run(
        <String>['bash-completion', outputFile],
      );
56 57
      expect(globals.fs.isFileSync(outputFile), isTrue);
      expect(globals.fs.file(outputFile).readAsStringSync(), contains('__flutter_completion'));
58
    }, overrides: <Type, Generator>{
59
      FileSystem: () => MemoryFileSystem.test(),
60
      ProcessManager: () => FakeProcessManager.any(),
61
      Stdio: () => fakeStdio,
62 63 64
    });

    testUsingContext("won't overwrite existing output file ", () async {
65
      final ShellCompletionCommand command = ShellCompletionCommand();
66
      const String outputFile = 'bash-setup.sh';
67
      globals.fs.file(outputFile).createSync();
68 69
      await expectLater(
        () => createTestCommandRunner(command).run(
70
          <String>['bash-completion', outputFile],
71 72 73 74 75 76 77
        ),
        throwsA(
          isA<ToolExit>()
            .having((ToolExit error) => error.exitCode, 'exitCode', anyOf(isNull, 1))
            .having((ToolExit error) => error.message, 'message', contains('Use --overwrite')),
        ),
      );
78 79
      expect(globals.fs.isFileSync(outputFile), isTrue);
      expect(globals.fs.file(outputFile).readAsStringSync(), isEmpty);
80
    }, overrides: <Type, Generator>{
81
      FileSystem: () => MemoryFileSystem.test(),
82
      ProcessManager: () => FakeProcessManager.any(),
83
      Stdio: () => fakeStdio,
84 85 86
    });

    testUsingContext('will overwrite existing output file if given --overwrite', () async {
87
      final ShellCompletionCommand command = ShellCompletionCommand();
88
      const String outputFile = 'bash-setup.sh';
89
      globals.fs.file(outputFile).createSync();
90 91 92
      await createTestCommandRunner(command).run(
        <String>['bash-completion', '--overwrite', outputFile],
      );
93 94
      expect(globals.fs.isFileSync(outputFile), isTrue);
      expect(globals.fs.file(outputFile).readAsStringSync(), contains('__flutter_completion'));
95
    }, overrides: <Type, Generator>{
96
      FileSystem: () => MemoryFileSystem.test(),
97
      ProcessManager: () => FakeProcessManager.any(),
98
      Stdio: () => fakeStdio,
99 100 101
    });
  });
}