source_test.dart 8.13 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
7
import 'package:flutter_tools/src/base/platform.dart';
8 9 10 11
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/build_system/build_system.dart';
import 'package:flutter_tools/src/build_system/exceptions.dart';
import 'package:flutter_tools/src/build_system/source.dart';
12
import 'package:flutter_tools/src/globals.dart' as globals;
13
import 'package:mockito/mockito.dart';
14 15

import '../../src/common.dart';
16
import '../../src/context.dart';
17 18
import '../../src/testbed.dart';

19 20 21 22
final Platform windowsPlatform = FakePlatform(
  operatingSystem: 'windows',
);

23 24 25 26 27 28 29
void main() {
  Testbed testbed;
  SourceVisitor visitor;
  Environment environment;

  setUp(() {
    testbed = Testbed(setup: () {
30 31
      globals.fs.directory('cache').createSync();
      final Directory outputs = globals.fs.directory('outputs')
32
          ..createSync();
33 34
      environment = Environment.test(
        globals.fs.currentDirectory,
35
        outputDir: outputs,
36 37 38 39
        artifacts: globals.artifacts, // using real artifacts
        processManager: FakeProcessManager.any(),
        fileSystem: globals.fs,
        logger: globals.logger,
40
        engineVersion: null, // simulate a local engine.
41 42 43 44 45 46 47 48 49 50 51 52
      );
      visitor = SourceVisitor(environment);
      environment.buildDir.createSync(recursive: true);
    });
  });

  test('configures implicit vs explict correctly', () => testbed.run(() {
    expect(const Source.pattern('{PROJECT_DIR}/foo').implicit, false);
    expect(const Source.pattern('{PROJECT_DIR}/*foo').implicit, true);
  }));

  test('can substitute {PROJECT_DIR}/foo', () => testbed.run(() {
53
    globals.fs.file('foo').createSync();
54 55 56
    const Source fooSource = Source.pattern('{PROJECT_DIR}/foo');
    fooSource.accept(visitor);

57
    expect(visitor.sources.single.path, globals.fs.path.absolute('foo'));
58 59
  }));

60
  test('can substitute {OUTPUT_DIR}/foo', () => testbed.run(() {
61
    globals.fs.file('foo').createSync();
62 63 64
    const Source fooSource = Source.pattern('{OUTPUT_DIR}/foo');
    fooSource.accept(visitor);

65
    expect(visitor.sources.single.path, globals.fs.path.absolute(globals.fs.path.join('outputs', 'foo')));
66 67 68
  }));


69
  test('can substitute {BUILD_DIR}/bar', () => testbed.run(() {
70 71
    final String path = globals.fs.path.join(environment.buildDir.path, 'bar');
    globals.fs.file(path).createSync();
72 73 74
    const Source barSource = Source.pattern('{BUILD_DIR}/bar');
    barSource.accept(visitor);

75
    expect(visitor.sources.single.path, globals.fs.path.absolute(path));
76 77 78
  }));

  test('can substitute {FLUTTER_ROOT}/foo', () => testbed.run(() {
79 80
    final String path = globals.fs.path.join(environment.flutterRootDir.path, 'foo');
    globals.fs.file(path).createSync();
81 82 83
    const Source barSource = Source.pattern('{FLUTTER_ROOT}/foo');
    barSource.accept(visitor);

84
    expect(visitor.sources.single.path, globals.fs.path.absolute(path));
85 86 87
  }));

  test('can substitute Artifact', () => testbed.run(() {
88 89
    final String path = globals.fs.path.join(
      globals.cache.getArtifactDirectory('engine').path,
90 91 92
      'windows-x64',
      'foo',
    );
93
    globals.fs.file(path).createSync(recursive: true);
94 95 96
    const Source fizzSource = Source.artifact(Artifact.windowsDesktopPath, platform: TargetPlatform.windows_x64);
    fizzSource.accept(visitor);

97
    expect(visitor.sources.single.resolveSymbolicLinksSync(), globals.fs.path.absolute(path));
98 99 100 101 102 103 104 105
  }));

  test('can substitute {PROJECT_DIR}/*.fizz', () => testbed.run(() {
    const Source fizzSource = Source.pattern('{PROJECT_DIR}/*.fizz');
    fizzSource.accept(visitor);

    expect(visitor.sources, isEmpty);

106 107
    globals.fs.file('foo.fizz').createSync();
    globals.fs.file('foofizz').createSync();
108 109 110 111


    fizzSource.accept(visitor);

112
    expect(visitor.sources.single.path, globals.fs.path.absolute('foo.fizz'));
113 114 115 116 117 118 119 120
  }));

  test('can substitute {PROJECT_DIR}/fizz.*', () => testbed.run(() {
    const Source fizzSource = Source.pattern('{PROJECT_DIR}/fizz.*');
    fizzSource.accept(visitor);

    expect(visitor.sources, isEmpty);

121 122
    globals.fs.file('fizz.foo').createSync();
    globals.fs.file('fizz').createSync();
123 124 125

    fizzSource.accept(visitor);

126
    expect(visitor.sources.single.path, globals.fs.path.absolute('fizz.foo'));
127 128 129 130 131 132 133 134 135
  }));


  test('can substitute {PROJECT_DIR}/a*bc', () => testbed.run(() {
    const Source fizzSource = Source.pattern('{PROJECT_DIR}/bc*bc');
    fizzSource.accept(visitor);

    expect(visitor.sources, isEmpty);

136 137
    globals.fs.file('bcbc').createSync();
    globals.fs.file('bc').createSync();
138 139 140

    fizzSource.accept(visitor);

141
    expect(visitor.sources.single.path, globals.fs.path.absolute('bcbc'));
142 143 144 145 146 147
  }));


  test('crashes on bad substitute of two **', () => testbed.run(() {
    const Source fizzSource = Source.pattern('{PROJECT_DIR}/*.*bar');

148
    globals.fs.file('abcd.bar').createSync();
149

Dan Field's avatar
Dan Field committed
150
    expect(() => fizzSource.accept(visitor), throwsA(isA<InvalidPatternException>()));
151 152 153
  }));


154
  test("can't substitute foo", () => testbed.run(() {
155 156
    const Source invalidBase = Source.pattern('foo');

Dan Field's avatar
Dan Field committed
157
    expect(() => invalidBase.accept(visitor), throwsA(isA<InvalidPatternException>()));
158
  }));
159 160 161 162

  test('can substitute optional files', () => testbed.run(() {
    const Source missingSource = Source.pattern('{PROJECT_DIR}/foo', optional: true);

163
    expect(globals.fs.file('foo').existsSync(), false);
164 165 166
    missingSource.accept(visitor);
    expect(visitor.sources, isEmpty);
  }));
167 168

  test('can resolve a missing depfile', () => testbed.run(() {
169
    visitor.visitDepfile('foo.d');
170 171 172 173 174 175 176 177 178

    expect(visitor.sources, isEmpty);
    expect(visitor.containsNewDepfile, true);
  }));

  test('can resolve a populated depfile', () => testbed.run(() {
    environment.buildDir.childFile('foo.d')
      .writeAsStringSync('a.dart : c.dart');

179
    visitor.visitDepfile('foo.d');
180 181 182 183
    expect(visitor.sources.single.path, 'c.dart');
    expect(visitor.containsNewDepfile, false);

    final SourceVisitor outputVisitor = SourceVisitor(environment, false);
184
    outputVisitor.visitDepfile('foo.d');
185 186 187 188 189 190 191 192 193

    expect(outputVisitor.sources.single.path, 'a.dart');
    expect(outputVisitor.containsNewDepfile, false);
  }));

  test('does not crash on completely invalid depfile', () => testbed.run(() {
    environment.buildDir.childFile('foo.d')
        .writeAsStringSync('hello, world');

194
    visitor.visitDepfile('foo.d');
195 196 197 198 199 200 201 202
    expect(visitor.sources, isEmpty);
    expect(visitor.containsNewDepfile, false);
  }));

  test('can parse depfile with windows paths', () => testbed.run(() {
    environment.buildDir.childFile('foo.d')
        .writeAsStringSync(r'a.dart: C:\\foo\\bar.txt');

203
    visitor.visitDepfile('foo.d');
204 205 206
    expect(visitor.sources.single.path, r'C:\foo\bar.txt');
    expect(visitor.containsNewDepfile, false);
  }, overrides: <Type, Generator>{
207
    Platform: () => windowsPlatform,
208 209 210 211 212 213
  }));

  test('can parse depfile with spaces in paths', () => testbed.run(() {
    environment.buildDir.childFile('foo.d')
        .writeAsStringSync(r'a.dart: foo\ bar.txt');

214
    visitor.visitDepfile('foo.d');
215 216 217
    expect(visitor.sources.single.path, r'foo bar.txt');
    expect(visitor.containsNewDepfile, false);
  }));
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

  test('Non-local engine builds use the engine.version file as an Artifact dependency', () => testbed.run(() {
    final MockArtifacts artifacts = MockArtifacts();
    final Environment environment = Environment.test(
      globals.fs.currentDirectory,
      artifacts: artifacts, // using real artifacts
      processManager: FakeProcessManager.any(),
      fileSystem: globals.fs,
      logger: globals.logger,
      engineVersion: 'abcdefghijklmon' // Use a versioned engine.
    );
    visitor = SourceVisitor(environment);

    const Source fizzSource = Source.artifact(Artifact.windowsDesktopPath, platform: TargetPlatform.windows_x64);
    fizzSource.accept(visitor);

    expect(visitor.sources.single.path, contains('engine.version'));
    verifyNever(artifacts.getArtifactPath(
      any, platform: anyNamed('platform'), mode: anyNamed('mode')));
  }));
238 239
}

240
class MockArtifacts extends Mock implements Artifacts {}