Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applied changes suggested in main repo #2

Merged
2 commits merged into from
Nov 27, 2023
Merged
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
1 change: 1 addition & 0 deletions packages/custom_lint_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ dev_dependencies:
build_runner: ^2.3.3
lint_visitor_generator:
path: ../lint_visitor_generator
package_config: ^2.1.0
test: ^1.22.2
70 changes: 67 additions & 3 deletions packages/custom_lint_core/test/configs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io' as io;
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:custom_lint_core/custom_lint_core.dart';
import 'package:package_config/package_config.dart';
import 'package:path/path.dart';
import 'package:test/test.dart';

Expand All @@ -19,10 +20,62 @@ File createAnalysisOptions(String content) {
return PhysicalResourceProvider.INSTANCE.getFile(ioFile.path);
}

Future<String> createTempProject(String projectName, String tempDirPath) async {
final projectPath = join(tempDirPath, projectName);

final dir = io.Directory(projectPath);
await dir.create();

final libPath = join(dir.path, 'lib');
await io.Directory(libPath).create();

final analysisOptionsPath = join(libPath, 'analysis_options.yaml');
await io.File(analysisOptionsPath).writeAsString('''
custom_lint:
rules:
- from_package
''');

return dir.path;
}

Future<void> patchPackageConfig(
String packageName,
String packagePath,
) async {
final currentPackageConfig = await findPackageConfig(io.Directory.current);
if (currentPackageConfig == null) {
throw Exception('Could not find package config');
}

final patchedPackages = currentPackageConfig.packages.toList()
..add(
Package(
packageName,
Uri.file('$packagePath/'),
packageUriRoot: Uri.parse('lib/'),
),
);

final patchedPackageConfig = PackageConfig(
patchedPackages,
extraData: currentPackageConfig.extraData,
);

await savePackageConfig(
patchedPackageConfig,
io.Directory.current,
);

addTearDown(
() async => savePackageConfig(currentPackageConfig, io.Directory.current),
);
solid-vovabeloded marked this conversation as resolved.
Show resolved Hide resolved
}

void main() {
late File includeFile;
late CustomLintConfigs includeConfig;
const testConfigUri = 'package:include_test_package/analysis_options.yaml';
const testPackageName = 'test_package_with_config';
setUp(() async {
includeFile = createAnalysisOptions(
'''
Expand Down Expand Up @@ -156,19 +209,30 @@ custom_lint:
});

test('include config using package: uri', () async {
final dir = createDir();
final file = createAnalysisOptions('''
include: $testConfigUri
include: package:$testPackageName/analysis_options.yaml
''');

final tempProjectDir = await createTempProject(dir.path, testPackageName);
await patchPackageConfig(testPackageName, tempProjectDir);
final configs = await CustomLintConfigs.parse(file);

expect(configs.rules.containsKey('from_package'), true);
});

test('if package: uri is not resolved default to empty', () async {
const notExistedFileName = 'this-does-not-exist';

final file = createAnalysisOptions('''
include: $testConfigUri/notexists
include: package:$testPackageName/$notExistedFileName
''');
final dir = createDir();

final tempProjectDir = await createTempProject(dir.path, testPackageName);
await patchPackageConfig(testPackageName, tempProjectDir);
final configs = await CustomLintConfigs.parse(file);

expect(configs, same(CustomLintConfigs.empty));
});

Expand Down
7 changes: 0 additions & 7 deletions packages/include_test_package/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions packages/include_test_package/lib/analysis_options.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions packages/include_test_package/pubspec.yaml

This file was deleted.