Skip to content

asset.dart subtly depends on a global via Cache.flutterRoot, inhibiting hermetic tests of asset bundle code  #142285

@andrewkolos

Description

@andrewkolos

ManifestAssetBundle accepts dependencies via its constructor to allow for hermetic testing of its behavior:

ManifestAssetBundle({
required Logger logger,
required FileSystem fileSystem,
required Platform platform,
bool splitDeferredAssets = false,
}) : _logger = logger,

However, we refer to the global Cache.flutterRoot in a couple places throughout the code:

final String shaderPath = _fileSystem.path.join(
Cache.flutterRoot!,
'packages', 'flutter', 'lib', 'src', 'material', 'shaders',
);

This means that tools test using testWithoutContext have to remember to manually set this global. Example:

late String? previousCacheFlutterRootValue;
setUp(() {
previousCacheFlutterRootValue = Cache.flutterRoot;
Cache.flutterRoot = Cache.defaultFlutterRoot(platform: platform, fileSystem: testFileSystem, userMessages: UserMessages());
});
tearDown(() => Cache.flutterRoot = previousCacheFlutterRootValue);

If nothing worse, this is noisy. Let's consider having ManifestAssetBundle accept the flutter root as a constructor parameter.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listc: tech-debtTechnical debt, code quality, testing, etc.team-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions