config_test.dart 6.07 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

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

  setUp(() {
22
    memoryFileSystem = MemoryFileSystem.test();
23 24 25 26 27 28 29 30
    fakePlatform = FakePlatform(
      environment: <String, String>{
        'HOME': '/',
      },
    );
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
31
      logger: BufferLogger.test(),
32 33
      platform: fakePlatform,
    );
34
  });
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  testWithoutContext('Config does not error on a file with a deprecated field', () {
    final BufferLogger bufferLogger = BufferLogger.test();
    final File file = memoryFileSystem.file('.flutter_example')
      ..writeAsStringSync('''
{
  "is-bot": false,
  "license-hash": "3e8c85e63b26ce223cda96a9a8fbb410",
  "redisplay-welcome-message": true,
  "last-devtools-activation-time": "2021-10-04 16:03:19.832823",
  "last-active-stable-version": "b22742018b3edf16c6cadd7b76d9db5e7f9064b5"
}
''');
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: bufferLogger,
      platform: fakePlatform,
    );

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

89
  testWithoutContext('Config parse error', () {
90
    final BufferLogger bufferLogger = BufferLogger.test();
91
    final File file = memoryFileSystem.file('.flutter_example')
92
      ..writeAsStringSync('{"hello":"bar');
93 94 95 96 97 98
    config = Config(
      'example',
      fileSystem: memoryFileSystem,
      logger: bufferLogger,
      platform: fakePlatform,
    );
99 100 101

    expect(file.existsSync(), false);
    expect(bufferLogger.errorText, contains('Failed to decode preferences'));
102
  });
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

  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();
120
    final Platform platform = FakePlatform();
121
    final File file = ErrorHandlingFile(
122 123
      platform: platform,
      fileSystem: ErrorHandlingFileSystem(delegate: MemoryFileSystem.test(), platform: platform),
124 125 126 127 128 129
      delegate: FakeFile('testfile'),
    );

    config = Config.createForTesting(file, bufferLogger);

    expect(bufferLogger.errorText, contains('Could not read preferences in testfile'));
130
    expect(bufferLogger.errorText, contains(r'sudo chown -R $(whoami) /testfile'));
131
  });
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  testWithoutContext('Config.createForTesting does not error when failing to delete a file', () {
    final BufferLogger bufferLogger = BufferLogger.test();

    final FileExceptionHandler handler = FileExceptionHandler();
    final MemoryFileSystem fs = MemoryFileSystem.test(opHandle: handler.opHandle);
    final File file = fs.file('testfile')
        // We write invalid JSON so that we test catching a `FormatException`
        ..writeAsStringSync('{"This is not valid JSON"');
    handler.addError(
      file,
      FileSystemOp.delete,
      const FileSystemException(
        "Cannot delete file, path = 'testfile' (OS Error: No such file or directory, errno = 2)",
      ),
    );

    // Should not throw a FileSystemException
    Config.createForTesting(file, bufferLogger);
  });

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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);
  });
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

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
  }
193
}