config_test.dart 4.59 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/error_handling_io.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11 12
import 'package:flutter_tools/src/convert.dart';
import 'package:test/fake.dart';
13

14
import '../src/common.dart';
15 16 17

void main() {
  Config config;
18
  MemoryFileSystem memoryFileSystem;
19
  FakePlatform fakePlatform;
20 21

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

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

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

56
  testWithoutContext('Config removeValue', () async {
57 58 59 60 61 62 63 64 65
    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')));
  });

66
  testWithoutContext('Config parse error', () {
67
    final BufferLogger bufferLogger = BufferLogger.test();
68
    final File file = memoryFileSystem.file('.flutter_example')
69
      ..writeAsStringSync('{"hello":"bar');
70 71 72 73 74 75
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: bufferLogger,
      platform: fakePlatform,
    );
76 77 78

    expect(file.existsSync(), false);
    expect(bufferLogger.errorText, contains('Failed to decode preferences'));
79
  });
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  testWithoutContext('Config does not error on missing file', () {
    final BufferLogger bufferLogger = BufferLogger.test();
    final File file = memoryFileSystem.file('example');
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: bufferLogger,
      platform: fakePlatform,
    );

    expect(file.existsSync(), false);
    expect(bufferLogger.errorText, isEmpty);
  });

  testWithoutContext('Config does not error on a normally fatal file system exception', () {
    final BufferLogger bufferLogger = BufferLogger.test();
    final File file = ErrorHandlingFile(
      platform: FakePlatform(operatingSystem: 'linux'),
      fileSystem: MemoryFileSystem.test(),
      delegate: FakeFile('testfile'),
    );

    config = Config.createForTesting(file, bufferLogger);

    expect(bufferLogger.errorText, contains('Could not read preferences in testfile'));
    // Also contains original error message:
    expect(bufferLogger.errorText, contains('The flutter tool cannot access the file or directory'));
  });
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  testWithoutContext('Config in home dir is used if it exists', () {
    memoryFileSystem.file('.flutter_example').writeAsStringSync('{"hello":"bar"}');
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: BufferLogger.test(),
      platform: fakePlatform,
    );
    expect(config.getValue('hello'), 'bar');
    expect(memoryFileSystem.file('.config/flutter/example').existsSync(), false);
  });

  testWithoutContext('Config is created in config dir if it does not already exist in home dir', () {
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: BufferLogger.test(),
      platform: fakePlatform,
    );

    config.setValue('foo', 'bar');
    expect(memoryFileSystem.file('.config/flutter/example').existsSync(), true);
  });
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
}

class FakeFile extends Fake implements File {
  FakeFile(this.path);

  @override
  final String path;

  @override
  bool existsSync() {
    return true;
  }

  @override
  String readAsStringSync({Encoding encoding = utf8ForTesting}) {
    throw const FileSystemException('', '', OSError('', 13)); // EACCES error on linux
  }
150
}