shell_completion_test.dart 3.91 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11
// 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
import 'package:flutter_tools/src/globals.dart' as globals;
14

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

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

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

    testUsingContext('generates bash initialization script to stdout', () async {
29
      final ShellCompletionCommand command = ShellCompletionCommand();
30 31 32 33 34 35 36 37
      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 {
38
      final ShellCompletionCommand command = ShellCompletionCommand();
39 40 41 42 43 44 45 46
      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 {
47
      final ShellCompletionCommand command = ShellCompletionCommand();
48 49 50 51
      const String outputFile = 'bash-setup.sh';
      await createTestCommandRunner(command).run(
        <String>['bash-completion', outputFile],
      );
52 53
      expect(globals.fs.isFileSync(outputFile), isTrue);
      expect(globals.fs.file(outputFile).readAsStringSync(), contains('__flutter_completion'));
54
    }, overrides: <Type, Generator>{
55
      FileSystem: () => MemoryFileSystem(),
56
      ProcessManager: () => FakeProcessManager.any(),
57 58 59 60
      Stdio: () => mockStdio,
    });

    testUsingContext("won't overwrite existing output file ", () async {
61
      final ShellCompletionCommand command = ShellCompletionCommand();
62
      const String outputFile = 'bash-setup.sh';
63
      globals.fs.file(outputFile).createSync();
64 65 66 67 68 69 70 71 72
      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'));
      }
73 74
      expect(globals.fs.isFileSync(outputFile), isTrue);
      expect(globals.fs.file(outputFile).readAsStringSync(), isEmpty);
75
    }, overrides: <Type, Generator>{
76
      FileSystem: () => MemoryFileSystem(),
77
      ProcessManager: () => FakeProcessManager.any(),
78 79 80 81
      Stdio: () => mockStdio,
    });

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