precache_test.dart 5.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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/cache.dart';
import 'package:flutter_tools/src/commands/precache.dart';
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';

11 12 13
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/mocks.dart';
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

void main() {
  group('precache', () {
    final MockCache cache = MockCache();
    Set<DevelopmentArtifact> artifacts;

    when(cache.isUpToDate()).thenReturn(false);
    when(cache.updateAll(any)).thenAnswer((Invocation invocation) {
      artifacts = invocation.positionalArguments.first;
      return Future<void>.value(null);
    });

    testUsingContext('Adds artifact flags to requested artifacts', () async {
      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(
30 31 32 33 34 35 36 37 38 39
        const <String>[
          'precache',
          '--ios',
          '--android',
          '--web',
          '--macos',
          '--linux',
          '--windows',
          '--fuchsia',
          '--flutter_runner',
40
        ],
41 42 43 44
      );
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
        DevelopmentArtifact.universal,
        DevelopmentArtifact.iOS,
45 46 47
        DevelopmentArtifact.androidGenSnapshot,
        DevelopmentArtifact.androidMaven,
        DevelopmentArtifact.androidInternalBuild,
48 49 50 51
        DevelopmentArtifact.web,
        DevelopmentArtifact.macOS,
        DevelopmentArtifact.linux,
        DevelopmentArtifact.windows,
52
        DevelopmentArtifact.fuchsia,
53
        DevelopmentArtifact.flutterRunner,
54 55 56 57 58
      }));
    }, overrides: <Type, Generator>{
      Cache: () => cache,
    });

59 60 61 62 63 64 65 66 67 68 69
    testUsingContext('Expands android artifacts when the android flag is used', () async {
      // Release lock between test cases.
      Cache.releaseLockEarly();

      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(
        const <String>[
          'precache',
          '--no-ios',
          '--android',
70
        ],
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      );
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
        DevelopmentArtifact.universal,
        DevelopmentArtifact.androidGenSnapshot,
        DevelopmentArtifact.androidMaven,
        DevelopmentArtifact.androidInternalBuild,
      }));
    }, overrides: <Type, Generator>{
      Cache: () => cache,
    });

    testUsingContext('Adds artifact flags to requested android artifacts', () async {
      // Release lock between test cases.
      Cache.releaseLockEarly();

      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(
        const <String>[
          'precache',
          '--no-ios',
          '--android_gen_snapshot',
          '--android_maven',
          '--android_internal_build',
95
        ],
96 97 98 99 100 101 102 103 104 105 106
      );
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
        DevelopmentArtifact.universal,
        DevelopmentArtifact.androidGenSnapshot,
        DevelopmentArtifact.androidMaven,
        DevelopmentArtifact.androidInternalBuild,
      }));
    }, overrides: <Type, Generator>{
      Cache: () => cache,
    });

107
    final MockFlutterVersion flutterVersion = MockFlutterVersion();
108
    when(flutterVersion.isMaster).thenReturn(false);
109 110 111 112 113 114 115

    testUsingContext('Adds artifact flags to requested artifacts on stable', () async {
      // Release lock between test cases.
      Cache.releaseLockEarly();
      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(
116
        const <String>[
117 118 119 120 121 122 123 124 125 126 127
          'precache',
          '--ios',
          '--android_gen_snapshot',
          '--android_maven',
          '--android_internal_build',
          '--web',
          '--macos',
          '--linux',
          '--windows',
          '--fuchsia',
          '--flutter_runner',
128
        ],
129
      );
130 131 132 133 134 135 136
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
        DevelopmentArtifact.universal,
        DevelopmentArtifact.iOS,
        DevelopmentArtifact.androidGenSnapshot,
        DevelopmentArtifact.androidMaven,
        DevelopmentArtifact.androidInternalBuild,
      }));
137 138 139 140
    }, overrides: <Type, Generator>{
      Cache: () => cache,
      FlutterVersion: () => flutterVersion,
    });
141 142 143 144 145 146 147 148
    testUsingContext('Downloads artifacts when --force is provided', () async {
      when(cache.isUpToDate()).thenReturn(true);
      // Release lock between test cases.
      Cache.releaseLockEarly();
      final PrecacheCommand command = PrecacheCommand();
      applyMocksToCommand(command);
      await createTestCommandRunner(command).run(const <String>['precache', '--force']);
      expect(artifacts, unorderedEquals(<DevelopmentArtifact>{
149 150 151 152 153 154
        DevelopmentArtifact.universal,
        DevelopmentArtifact.iOS,
        DevelopmentArtifact.androidGenSnapshot,
        DevelopmentArtifact.androidMaven,
        DevelopmentArtifact.androidInternalBuild,
      }));
155 156 157 158
    }, overrides: <Type, Generator>{
      Cache: () => cache,
      FlutterVersion: () => flutterVersion,
    });
159 160 161 162 163
  });
}

class MockFlutterVersion extends Mock implements FlutterVersion {}
class MockCache extends Mock implements Cache {}