Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions packages/core/lib/settings/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Config loadPubspecConfig(File pubspecFile, {File? buildFile}) {
final pubspec = Pubspec.fromJson(mergedMap);

final pubspecLockFile = File(
normalize(join(basename(pubspecFile.parent.path), 'pubspec.lock')),
normalize(join(pubspecFile.parent.path, 'pubspec.lock')),
);
final pubspecLockContent = switch (pubspecLockFile.existsSync()) {
true => pubspecLockFile.readAsStringSync(),
Expand All @@ -130,7 +130,7 @@ Config loadPubspecConfig(File pubspecFile, {File? buildFile}) {
}

final analysisOptionsFile = File(
normalize(join(basename(pubspecFile.parent.path), 'analysis_options.yaml')),
normalize(join(pubspecFile.parent.path, 'analysis_options.yaml')),
);
final analysisOptionsContent = switch (analysisOptionsFile.existsSync()) {
true => analysisOptionsFile.readAsStringSync(),
Expand Down
74 changes: 74 additions & 0 deletions packages/core/test/assets_gen_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,5 +459,79 @@ void main() {
isTrue,
);
});

test('Integration version resolution in AssetsGenConfig', () {
// Test that version resolution is properly integrated into assets generation
// This validates the full flow from config loading to integration creation

// Create a RiveIntegration with both resolved version and constraint
// When resolvedVersion is 0.13.0 (< 0.14.0), it should use Classic
// even if resolvedVersionConstraint would allow 0.14.0
final integrationWithBoth = RiveIntegration(
'',
resolvedVersion: Version(0, 13, 0),
resolvedVersionConstraint: VersionConstraint.parse('^0.14.0'),
);
// resolvedVersion takes priority: 0.13.0 < 0.14.0 => Classic
expect(integrationWithBoth, isA<RiveIntegrationClassic>());

// Create a RiveIntegration with only resolvedVersion
final integrationVersionOnly = RiveIntegration(
'',
resolvedVersion: Version(0, 14, 0),
);
expect(integrationVersionOnly, isA<RiveIntegration0140>());

// Create a RiveIntegration with only resolvedVersionConstraint
final integrationConstraintOnly = RiveIntegration(
'',
resolvedVersionConstraint: VersionConstraint.parse('^0.14.0'),
);
expect(integrationConstraintOnly, isA<RiveIntegration0140>());
});

test('Version resolution with various constraint formats', () {
// Test with ^0.13.x constraint (should use Classic)
final caretOld = RiveIntegration(
'',
resolvedVersionConstraint: VersionConstraint.parse('^0.13.5'),
);
expect(caretOld, isA<RiveIntegrationClassic>());

// Test with ^0.14.x constraint (should use 0140)
final caretNew = RiveIntegration(
'',
resolvedVersionConstraint: VersionConstraint.parse('^0.14.1'),
);
expect(caretNew, isA<RiveIntegration0140>());

// Test with >= constraint that includes 0.14.0
final rangeIncludesNew = RiveIntegration(
'',
resolvedVersionConstraint: VersionConstraint.parse('>=0.13.0 <1.0.0'),
);
expect(rangeIncludesNew, isA<RiveIntegration0140>());

// Test with >= constraint that excludes 0.14.0
final rangeExcludesNew = RiveIntegration(
'',
resolvedVersionConstraint: VersionConstraint.parse('>=0.13.0 <0.14.0'),
);
expect(rangeExcludesNew, isA<RiveIntegrationClassic>());

// Test with exact version < 0.14.0
final exactOld = RiveIntegration(
'',
resolvedVersion: Version(0, 13, 99),
);
expect(exactOld, isA<RiveIntegrationClassic>());

// Test with exact version >= 0.14.0
final exactNew = RiveIntegration(
'',
resolvedVersion: Version(0, 14, 0),
);
expect(exactNew, isA<RiveIntegration0140>());
});
});
}
231 changes: 231 additions & 0 deletions packages/core/test/config_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import 'dart:io';

import 'package:flutter_gen_core/generators/integrations/lottie_integration.dart';
import 'package:flutter_gen_core/generators/integrations/rive_integration.dart';
import 'package:flutter_gen_core/generators/integrations/svg_integration.dart';
import 'package:flutter_gen_core/generators/registry.dart';
import 'package:flutter_gen_core/settings/config.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';

void main() {
group('Config integration version resolution', () {
test('resolves versions from pubspec.lock', () {
final pubspecFile = File(
'test_resources/integration_versions/pubspec.yaml',
);
final config = loadPubspecConfig(pubspecFile);

// Check that versions are resolved from pubspec.lock
expect(
config.integrationResolvedVersions[RiveIntegration],
equals(Version(0, 13, 5)),
);
expect(
config.integrationResolvedVersions[SvgIntegration],
equals(Version(2, 0, 9)),
);
expect(
config.integrationResolvedVersions[LottieIntegration],
equals(Version(5, 1, 0)),
);
});

test('resolves version constraints from pubspec.yaml', () {
final pubspecFile = File(
'test_resources/integration_versions/pubspec.yaml',
);
final config = loadPubspecConfig(pubspecFile);

// Check that constraints are resolved from pubspec.yaml
expect(
config.integrationVersionConstraints[RiveIntegration],
equals(VersionConstraint.parse('^0.13.0')),
);
expect(
config.integrationVersionConstraints[SvgIntegration],
equals(VersionConstraint.parse('^2.0.0')),
);
expect(
config.integrationVersionConstraints[LottieIntegration],
equals(VersionConstraint.parse('^5.0.0')),
);
});

test('resolves Rive 0.14.0 versions correctly', () {
final pubspecFile = File(
'test_resources/integration_versions_rive_014/pubspec.yaml',
);
final config = loadPubspecConfig(pubspecFile);

// Check that Rive 0.14.0+ version is resolved
expect(
config.integrationResolvedVersions[RiveIntegration],
equals(Version(0, 14, 1)),
);
expect(
config.integrationVersionConstraints[RiveIntegration],
equals(VersionConstraint.parse('^0.14.0')),
);

// Check that flutter_svg is also resolved
expect(
config.integrationResolvedVersions[SvgIntegration],
equals(Version(2, 0, 10)),
);
expect(
config.integrationVersionConstraints[SvgIntegration],
equals(VersionConstraint.parse('>=2.0.0 <3.0.0')),
);
});

test('handles missing pubspec.lock gracefully', () {
// Use a pubspec without a corresponding .lock file
final pubspecFile = File(
'test_resources/pubspec_assets_rive_integrations.yaml',
);
final config = loadPubspecConfig(pubspecFile);

// Should have empty or no resolved versions
// but should not crash
expect(config, isNotNull);
expect(config.integrationResolvedVersions, isA<Map>());
expect(config.integrationVersionConstraints, isA<Map>());
});

test('verifies only expected integration types are present', () {
final pubspecFile = File(
'test_resources/integration_versions/pubspec.yaml',
);
final config = loadPubspecConfig(pubspecFile);

// Verify that only integration types from the registry are in the maps
final expectedTypes = integrationPackages.keys.toList();

for (final key in config.integrationVersionConstraints.keys) {
expect(
expectedTypes.contains(key),
isTrue,
reason: 'Unexpected integration type: $key',
);
}

for (final key in config.integrationResolvedVersions.keys) {
expect(
expectedTypes.contains(key),
isTrue,
reason: 'Unexpected integration type: $key',
);
}
});

test('integration versions are used in AssetsGenConfig', () {
final pubspecFile = File(
'test_resources/integration_versions/pubspec.yaml',
);
final config = loadPubspecConfig(pubspecFile);

// Verify that the resolved versions and constraints are available
expect(config.integrationResolvedVersions, isNotEmpty);
expect(config.integrationVersionConstraints, isNotEmpty);

// Verify they can be passed to generators
expect(
config.integrationResolvedVersions[RiveIntegration],
isA<Version>(),
);
expect(
config.integrationVersionConstraints[RiveIntegration],
isA<VersionConstraint>(),
);
});

test('version resolution with only constraint and no lock', () {
// Create a temporary pubspec file without a lock
final tempDir = Directory.systemTemp.createTempSync('flutter_gen_test');
try {
final tempPubspec = File('${tempDir.path}/pubspec.yaml');
tempPubspec.writeAsStringSync('''
name: test_no_lock
environment:
sdk: ^3.0.0
dependencies:
rive: ^0.13.0
flutter_gen:
output: lib/gen/
integrations:
rive: true
flutter:
assets:
- assets/
''');

final config = loadPubspecConfig(tempPubspec);

// Should have constraint but no resolved version
expect(
config.integrationVersionConstraints[RiveIntegration],
equals(VersionConstraint.parse('^0.13.0')),
);
expect(
config.integrationResolvedVersions[RiveIntegration],
isNull,
);
} finally {
tempDir.deleteSync(recursive: true);
}
});

test('version resolution with lock but no constraint', () {
// This tests the case where pubspec.lock has a version
// but pubspec.yaml doesn't specify a constraint (e.g., path dependency)
final tempDir = Directory.systemTemp.createTempSync('flutter_gen_test');
try {
final tempPubspec = File('${tempDir.path}/pubspec.yaml');
tempPubspec.writeAsStringSync('''
name: test_lock_only
environment:
sdk: ^3.0.0
dependencies:
rive:
path: ../rive
flutter_gen:
output: lib/gen/
integrations:
rive: true
flutter:
assets:
- assets/
''');

final tempLock = File('${tempDir.path}/pubspec.lock');
tempLock.writeAsStringSync('''
packages:
rive:
dependency: "direct main"
description:
path: "../rive"
relative: true
source: path
version: "0.13.5"
sdks:
dart: ">=3.0.0 <4.0.0"
''');

final config = loadPubspecConfig(tempPubspec);

// Should have resolved version but no constraint
expect(
config.integrationResolvedVersions[RiveIntegration],
equals(Version(0, 13, 5)),
);
expect(
config.integrationVersionConstraints[RiveIntegration],
isNull,
);
} finally {
tempDir.deleteSync(recursive: true);
}
});
});
}
Loading
Loading