config_test.dart 2.48 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
import 'package:file/memory.dart';
6
import 'package:flutter_tools/src/base/config.dart';
7
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9 10
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:platform/platform.dart';
11

12
import '../src/common.dart';
13 14 15

void main() {
  Config config;
16
  MemoryFileSystem memoryFileSystem;
17
  FakePlatform fakePlatform;
18 19

  setUp(() {
20
    memoryFileSystem = MemoryFileSystem();
21 22 23 24 25 26 27 28 29
    fakePlatform = FakePlatform(
      operatingSystem: 'linux',
      environment: <String, String>{
        'HOME': '/',
      },
    );
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
30
      logger: BufferLogger.test(),
31 32
      platform: fakePlatform,
    );
33
  });
34
  testWithoutContext('Config get set value', () async {
35 36 37 38 39 40
    expect(config.getValue('foo'), null);
    config.setValue('foo', 'bar');
    expect(config.getValue('foo'), 'bar');
    expect(config.keys, contains('foo'));
  });

41
  testWithoutContext('Config get set bool value', () async {
42 43 44 45 46
    expect(config.getValue('foo'), null);
    config.setValue('foo', true);
    expect(config.getValue('foo'), true);
    expect(config.keys, contains('foo'));
  });
47

48
  testWithoutContext('Config containsKey', () async {
49 50 51
    expect(config.containsKey('foo'), false);
    config.setValue('foo', 'bar');
    expect(config.containsKey('foo'), true);
52 53
  });

54
  testWithoutContext('Config removeValue', () async {
55 56 57 58 59 60 61 62 63
    expect(config.getValue('foo'), null);
    config.setValue('foo', 'bar');
    expect(config.getValue('foo'), 'bar');
    expect(config.keys, contains('foo'));
    config.removeValue('foo');
    expect(config.getValue('foo'), null);
    expect(config.keys, isNot(contains('foo')));
  });

64
  testWithoutContext('Config parse error', () {
65 66 67 68 69 70 71
    final BufferLogger bufferLogger = BufferLogger(
      terminal: AnsiTerminal(
        stdio: null,
        platform: const LocalPlatform(),
      ),
      outputPreferences: OutputPreferences.test(),
    );
72 73
    final File file = memoryFileSystem.file('example')
      ..writeAsStringSync('{"hello":"bar');
74 75 76 77 78 79
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: bufferLogger,
      platform: fakePlatform,
    );
80 81 82

    expect(file.existsSync(), false);
    expect(bufferLogger.errorText, contains('Failed to decode preferences'));
83 84
  });
}