shell_completion_test.dart 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2018 The Chromium 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:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/context.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';
12
import 'package:process/process.dart';
13

14 15 16
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/mocks.dart';
17 18 19 20 21 22 23

void main() {
  group('shell_completion', () {
    MockStdio mockStdio;

    setUp(() {
      Cache.disableLocking();
24
      mockStdio = MockStdio();
25 26 27
    });

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

    testUsingContext('generates bash initialization script to stdout with arg', () async {
37
      final ShellCompletionCommand command = ShellCompletionCommand();
38 39 40 41 42 43 44 45
      await createTestCommandRunner(command).run(<String>['bash-completion', '-']);
      expect(mockStdio.writtenToStdout.length, equals(1));
      expect(mockStdio.writtenToStdout.first, contains('__flutter_completion'));
    }, overrides: <Type, Generator>{
      Stdio: () => mockStdio,
    });

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

    testUsingContext("won't overwrite existing output file ", () async {
60
      final ShellCompletionCommand command = ShellCompletionCommand();
61 62 63 64 65 66 67 68 69 70 71 72 73 74
      const String outputFile = 'bash-setup.sh';
      fs.file(outputFile).createSync();
      try {
        await createTestCommandRunner(command).run(
          <String>['bash-completion', outputFile],
        );
        fail('Expect ToolExit exception');
      } on ToolExit catch (error) {
        expect(error.exitCode ?? 1, 1);
        expect(error.message, contains('Use --overwrite'));
      }
      expect(fs.isFileSync(outputFile), isTrue);
      expect(fs.file(outputFile).readAsStringSync(), isEmpty);
    }, overrides: <Type, Generator>{
75
      FileSystem: () => MemoryFileSystem(),
76
      ProcessManager: () => FakeProcessManager.any(),
77 78 79 80
      Stdio: () => mockStdio,
    });

    testUsingContext('will overwrite existing output file if given --overwrite', () async {
81
      final ShellCompletionCommand command = ShellCompletionCommand();
82 83 84 85 86 87 88 89
      const String outputFile = 'bash-setup.sh';
      fs.file(outputFile).createSync();
      await createTestCommandRunner(command).run(
        <String>['bash-completion', '--overwrite', outputFile],
      );
      expect(fs.isFileSync(outputFile), isTrue);
      expect(fs.file(outputFile).readAsStringSync(), contains('__flutter_completion'));
    }, overrides: <Type, Generator>{
90
      FileSystem: () => MemoryFileSystem(),
91
      ProcessManager: () => FakeProcessManager.any(),
92 93 94 95
      Stdio: () => mockStdio,
    });
  });
}