Visitar URL original
[flutter_tools] Support flavors and transformers for shaders by Laurie-Lin · Pull Request #181889 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
flutter_tools: add tests for shader transformers
  • Loading branch information
Laurie-Lin committed Feb 4, 2026
commit 62b976d9b6c316e852556c449f0783a4a3844d6c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,133 @@ flutter:
},
);

testUsingContext(
'transforms shaders declared with transformers before compilation',
() async {
Cache.flutterRoot = Cache.defaultFlutterRoot(
platform: globals.platform,
fileSystem: fileSystem,
userMessages: UserMessages(),
);

final environment = Environment.test(
fileSystem.currentDirectory,
processManager: globals.processManager,
artifacts: Artifacts.test(),
fileSystem: fileSystem,
logger: logger,
platform: globals.platform,
defines: <String, String>{kBuildMode: BuildMode.debug.cliName},
);

final Artifacts artifacts = Artifacts.test();
final String impellercPath = artifacts.getHostArtifact(HostArtifact.impellerc).path;
fileSystem.file(impellercPath).createSync(recursive: true);

fileSystem.file('pubspec.yaml')
..createSync()
..writeAsStringSync('''
name: example
flutter:
shaders:
- path: shaders/test.frag
transformers:
- package: my_capitalizer_transformer
''');

writePackageConfigFiles(directory: globals.fs.currentDirectory, mainLibName: 'example');

fileSystem.file('shaders/test.frag')
..createSync(recursive: true)
..writeAsStringSync('abc');

await const CopyAssets().build(environment);

expect(logger.errorText, isEmpty);
expect(globals.processManager, hasNoRemainingExpectations);
expect(
fileSystem.file('${environment.buildDir.path}/flutter_assets/shaders/test.frag'),
exists,
);
},
overrides: <Type, Generator>{
Logger: () => logger,
FileSystem: () => fileSystem,
Platform: () => FakePlatform(),
ProcessManager: () {
final Artifacts artifacts = Artifacts.test();
final String impellercPath = artifacts.getHostArtifact(HostArtifact.impellerc).path;

return FakeProcessManager.list(<FakeCommand>[
FakeCommand(
command: <Pattern>[
artifacts.getArtifactPath(Artifact.engineDartBinary),
'run',
'my_capitalizer_transformer',
RegExp('--input=.*'),
RegExp('--output=.*'),
],
onRun: (List<String> args) {
final ArgResults parsedArgs = (ArgParser()
..addOption('input')
..addOption('output'))
.parse(args);

final File input = fileSystem.file(parsedArgs['input'] as String);
expect(input, exists);
expect(input.readAsStringSync(), 'abc');

fileSystem.file(parsedArgs['output'])
..createSync(recursive: true)
..writeAsStringSync('ABC');
},
),
FakeCommand(
command: <Pattern>[
impellercPath,
'--sksl',
'--runtime-stage-gles',
'--runtime-stage-gles3',
'--runtime-stage-vulkan',
'--iplr',
RegExp(r'--sl=.*/flutter_assets/shaders/test\.frag$'),
RegExp(r'--spirv=.*/flutter_assets/shaders/test\.frag\.spirv$'),
RegExp(r'--input=.*/flutter_assets/shaders/test\.frag\.transformed$'),
'--input-type=frag',
RegExp(r'--include=.*/flutter_assets/shaders$'),
RegExp(r'--include=.*/shader_lib$'),
],
onRun: (List<String> args) {
String? inputPath;
String? outputPath;
String? outputSpirvPath;
for (final String arg in args) {
if (arg.startsWith('--input=')) {
inputPath = arg.substring('--input='.length);
} else if (arg.startsWith('--sl=')) {
outputPath = arg.substring('--sl='.length);
} else if (arg.startsWith('--spirv=')) {
outputSpirvPath = arg.substring('--spirv='.length);
}
}

expect(inputPath, isNotNull);
expect(outputPath, isNotNull);
expect(outputSpirvPath, isNotNull);

final File input = fileSystem.file(inputPath!);
expect(input, exists);
expect(input.readAsStringSync(), 'ABC');

fileSystem.file(outputPath!).createSync(recursive: true);
fileSystem.file(outputSpirvPath!).createSync(recursive: true);
},
),
]);
},
},
);

testUsingContext(
'exits tool if an asset transformation fails',
() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,30 @@ flutter:
expect(entry.platforms, isEmpty);
});

testWithoutContext('FlutterManifest parses shaders with transformers', () async {
const manifest = '''
name: test
flutter:
shaders:
- path: shaders/test.frag
transformers:
- package: my_transformer
args: ["--foo", "bar"]
''';

final FlutterManifest flutterManifest = FlutterManifest.createFromString(
manifest,
logger: logger,
)!;

expect(flutterManifest.shaders, hasLength(1));
final AssetsEntry shader = flutterManifest.shaders.single;
expect(shader.uri.path, 'shaders/test.frag');
expect(shader.transformers, hasLength(1));
expect(shader.transformers.single.package, 'my_transformer');
expect(shader.transformers.single.args, <String>['--foo', 'bar']);
});

testWithoutContext('FlutterManifest.copyWith generates a valid manifest', () async {
const manifest = '''
name: test
Expand Down Expand Up @@ -1673,7 +1697,7 @@ flutter:
FontAsset(Uri(path: 'assetUri'), weight: 100, style: 'normal'),
]),
],
shaders: <Uri>[Uri(path: 'shaderUri')],
shaders: <AssetsEntry>[AssetsEntry(uri: Uri(path: 'shaderUri'))],
deferredComponents: <DeferredComponent>[
DeferredComponent(
name: 'deferredComponent',
Expand Down