|
| 1 | +import 'package:analyzer/dart/analysis/analysis_context.dart'; |
| 2 | +import 'package:analyzer/dart/analysis/context_root.dart'; |
| 3 | +import 'package:analyzer/file_system/file_system.dart'; |
| 4 | +import 'package:dart_code_metrics/src/utils/analyzer_utils.dart'; |
| 5 | +import 'package:glob/glob.dart'; |
| 6 | +import 'package:mocktail/mocktail.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +class AnalysisContextMock extends Mock implements AnalysisContext {} |
| 10 | + |
| 11 | +class ContextRootMock extends Mock implements ContextRoot {} |
| 12 | + |
| 13 | +class FolderMock extends Mock implements Folder {} |
| 14 | + |
| 15 | +void main() { |
| 16 | + group('getFilePaths', () { |
| 17 | + const folderPath = 'file_paths_folder'; |
| 18 | + const rootFolder = 'test/resources'; |
| 19 | + |
| 20 | + final folder = FolderMock(); |
| 21 | + final contextRoot = ContextRootMock(); |
| 22 | + final context = AnalysisContextMock(); |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + when(() => folder.path).thenReturn('test/resources'); |
| 26 | + when(() => contextRoot.root).thenReturn(folder); |
| 27 | + when(() => context.contextRoot).thenReturn(contextRoot); |
| 28 | + }); |
| 29 | + |
| 30 | + test( |
| 31 | + 'should return paths to the files', |
| 32 | + () { |
| 33 | + const excludes = <Glob>[]; |
| 34 | + |
| 35 | + final filePaths = |
| 36 | + getFilePaths([folderPath], context, rootFolder, excludes).toList() |
| 37 | + ..sort(); |
| 38 | + |
| 39 | + expect(filePaths, hasLength(3)); |
| 40 | + |
| 41 | + const startPath = '$rootFolder/$folderPath'; |
| 42 | + |
| 43 | + final firstPath = filePaths.first; |
| 44 | + expect(firstPath, '$startPath/first_file.dart'); |
| 45 | + |
| 46 | + final secondPath = filePaths.elementAt(1); |
| 47 | + expect(secondPath, '$startPath/inner_folder/first_inner_file.dart'); |
| 48 | + |
| 49 | + final thirdPath = filePaths.last; |
| 50 | + expect(thirdPath, '$startPath/second_file.dart'); |
| 51 | + }, |
| 52 | + testOn: 'posix', |
| 53 | + ); |
| 54 | + |
| 55 | + test( |
| 56 | + 'should return paths to the files without excluded', |
| 57 | + () { |
| 58 | + final excludes = [Glob('**/second_file.dart')]; |
| 59 | + |
| 60 | + final filePaths = |
| 61 | + getFilePaths([folderPath], context, rootFolder, excludes).toList() |
| 62 | + ..sort(); |
| 63 | + |
| 64 | + expect(filePaths, hasLength(2)); |
| 65 | + |
| 66 | + const startPath = '$rootFolder/$folderPath'; |
| 67 | + |
| 68 | + final firstPath = filePaths.first; |
| 69 | + expect(firstPath, '$startPath/first_file.dart'); |
| 70 | + |
| 71 | + final secondPath = filePaths.last; |
| 72 | + expect(secondPath, '$startPath/inner_folder/first_inner_file.dart'); |
| 73 | + }, |
| 74 | + testOn: 'posix', |
| 75 | + ); |
| 76 | + }); |
| 77 | +} |
0 commit comments