-
Notifications
You must be signed in to change notification settings - Fork 3
fix(config): Validation Logic for Configuration File Extension #76
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
586ee8e
fix(ConfigurationParser): File Extension validation logic
indraneel12 a3c4c25
test: Configuration Loading from external files
indraneel12 ad33d06
docs: Comprehensive documentation for `ConfigurationParser.fromFile`
indraneel12 b276d4a
fix: Minor typo in an `assert` message
indraneel12 a2e7722
style: Potential noise removed
indraneel12 0a24eec
fix: Test Cases with Cross-platform compatibility
indraneel12 c817aab
refactor(ConfigurationParser): Better readability
indraneel12 42cec48
refactor(config_file_load_test): Better readability
indraneel12 1c86d6e
refactor(config_file_load_test): Given-When-Then format
indraneel12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| import 'dart:io' show File; | ||
|
|
||
| import 'package:config/config.dart' show ConfigurationParser; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() => _runTests(); | ||
|
|
||
| enum _Fact { correct, wrong } | ||
|
|
||
| enum _AllowedFile { json, yaml } | ||
|
|
||
| const _mockContent = { | ||
| _AllowedFile.json: { | ||
| _Fact.correct: '{"mock-key": "mock val"}', | ||
| _Fact.wrong: '{"mock-key": mock val}', | ||
| }, | ||
| _AllowedFile.yaml: { | ||
| _Fact.correct: 'mock-key: "mock val"', | ||
| _Fact.wrong: 'mock-key: mock val key2: value2', | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }; | ||
|
|
||
| const _mockKey = '/mock-key'; | ||
| const _mockVal = 'mock val'; | ||
|
|
||
| const _mockFilenames = { | ||
| _AllowedFile.json: { | ||
| _Fact.correct: 'mock_correct_json_file', | ||
| _Fact.wrong: 'mock_wrong_json_file', | ||
| }, | ||
| _AllowedFile.yaml: { | ||
| _Fact.correct: 'mock_correct_yaml_file', | ||
| _Fact.wrong: 'mock_wrong_yaml_file', | ||
| }, | ||
| }; | ||
|
|
||
| const _mockExtensions = { | ||
| _AllowedFile.json: [ | ||
| 'json', | ||
| 'jsoN', | ||
| 'jsOn', | ||
| 'jsON', | ||
| 'jSon', | ||
| 'jSoN', | ||
| 'jSOn', | ||
| 'jSON', | ||
| 'Json', | ||
| 'JsoN', | ||
| 'JsOn', | ||
| 'JsON', | ||
| 'JSon', | ||
| 'JSoN', | ||
| 'JSOn', | ||
| 'JSON', | ||
| ], | ||
| _AllowedFile.yaml: [ | ||
| 'yml', | ||
| 'ymL', | ||
| 'yMl', | ||
| 'yML', | ||
| 'Yml', | ||
| 'YmL', | ||
| 'YMl', | ||
| 'YML', | ||
| 'yaml', | ||
| 'yamL', | ||
| 'yaMl', | ||
| 'yaML', | ||
| 'yAml', | ||
| 'yAmL', | ||
| 'yAMl', | ||
| 'yAML', | ||
| 'Yaml', | ||
| 'YamL', | ||
| 'YaMl', | ||
| 'YaML', | ||
| 'YAml', | ||
| 'YAmL', | ||
| 'YAMl', | ||
| 'YAML', | ||
| ], | ||
| }; | ||
|
|
||
| const _anUnsupportedExtension = 'txt'; | ||
|
|
||
| final _mockDir = './test_tmp_${DateTime.now().millisecondsSinceEpoch}'; | ||
| final _aNonExistentFilename = 'xyz_${DateTime.now().millisecondsSinceEpoch}'; | ||
|
|
||
| String _buildFilepath( | ||
| final _Fact fact, | ||
| final _AllowedFile format, | ||
| final String extension, | ||
| ) => | ||
| '$_mockDir/${_mockFilenames[format]![fact]!}.$extension'; | ||
|
|
||
| void _prepareMockFiles() { | ||
| _mockFilenames.forEach((final fileFormat, final namesMap) { | ||
| for (final fact in _Fact.values) { | ||
| for (final fileExtension in _mockExtensions[fileFormat]!) { | ||
| File(_buildFilepath(fact, fileFormat, fileExtension)) | ||
| ..createSync(recursive: true, exclusive: false) | ||
| ..writeAsStringSync(_mockContent[fileFormat]![fact]!); | ||
| } | ||
| File(_buildFilepath(fact, fileFormat, _anUnsupportedExtension)) | ||
| ..createSync(recursive: true, exclusive: false) | ||
| ..writeAsStringSync(_mockContent[fileFormat]![fact]!); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void _cleanupMockFiles() => File(_mockDir).deleteSync(recursive: true); | ||
|
|
||
| void _runTests() { | ||
| setUpAll(() => _prepareMockFiles()); | ||
| tearDownAll(() => _cleanupMockFiles()); | ||
|
|
||
| group('Given correct file content', () { | ||
| group('with a supported file extension', () { | ||
| group('when loading a JSON file', () { | ||
| test('then it is parsed successfully', () { | ||
| for (final jsonExtension in _mockExtensions[_AllowedFile.json]!) { | ||
| expect( | ||
| ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.correct, | ||
| _AllowedFile.json, | ||
| jsonExtension, | ||
| )).valueOrNull(_mockKey), | ||
| equals(_mockVal), | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| group('when loading a YAML file', () { | ||
| test('then it is parsed successfully', () { | ||
| for (final yamlExtension in _mockExtensions[_AllowedFile.yaml]!) { | ||
| expect( | ||
| ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.correct, | ||
| _AllowedFile.yaml, | ||
| yamlExtension, | ||
| )).valueOrNull(_mockKey), | ||
| equals(_mockVal), | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| group('with an unsupported file extension', () { | ||
| group('when loading a JSON file', () { | ||
| test('then it reports an Unsupported Error', () { | ||
| expect( | ||
| () => ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.correct, | ||
| _AllowedFile.json, | ||
| _anUnsupportedExtension, | ||
| )), | ||
| throwsUnsupportedError, | ||
| ); | ||
| }); | ||
| }); | ||
| group('when loading a YAML file', () { | ||
| test('then it reports an Unsupported Error', () { | ||
| expect( | ||
| () => ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.correct, | ||
| _AllowedFile.yaml, | ||
| _anUnsupportedExtension, | ||
| )), | ||
| throwsUnsupportedError, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| group('Given wrong file content', () { | ||
| group('with a supported file extension', () { | ||
| group('when loading a JSON file', () { | ||
| test('then it reports a Format Exception', () { | ||
| for (final jsonExtension in _mockExtensions[_AllowedFile.json]!) { | ||
| expect( | ||
| () => ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.wrong, | ||
| _AllowedFile.json, | ||
| jsonExtension, | ||
| )), | ||
| throwsFormatException, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| group('when loading a YAML file', () { | ||
| test('then it reports a Format Exception', () { | ||
| for (final yamlExtension in _mockExtensions[_AllowedFile.yaml]!) { | ||
| expect( | ||
| () => ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.wrong, | ||
| _AllowedFile.yaml, | ||
| yamlExtension, | ||
| )), | ||
| throwsFormatException, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| group('with an unsupported file extension', () { | ||
| group('when loading a JSON file', () { | ||
| test('then it reports an Unsupported Error', () { | ||
| expect( | ||
| () => ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.wrong, | ||
| _AllowedFile.json, | ||
| _anUnsupportedExtension, | ||
| )), | ||
| throwsUnsupportedError, | ||
| ); | ||
| }); | ||
| }); | ||
| group('when loading a YAML file', () { | ||
| test('then it reports an Unsupported Error', () { | ||
| expect( | ||
| () => ConfigurationParser.fromFile(_buildFilepath( | ||
| _Fact.wrong, | ||
| _AllowedFile.yaml, | ||
| _anUnsupportedExtension, | ||
| )), | ||
| throwsUnsupportedError, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| group('Given a non-existent file', () { | ||
| group('with a supported file extension', () { | ||
| group('when attempting to load a JSON file', () { | ||
| test('then it reports an Argument Error', () { | ||
| for (final jsonExtension in _mockExtensions[_AllowedFile.json]!) { | ||
| expect( | ||
| () => ConfigurationParser.fromFile( | ||
| '$_aNonExistentFilename.$jsonExtension', | ||
| ), | ||
| throwsArgumentError, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| group('when attempting to load a YAML file', () { | ||
| test('then it reports an Argument Error', () { | ||
| for (final yamlExtension in _mockExtensions[_AllowedFile.yaml]!) { | ||
| expect( | ||
| () => ConfigurationParser.fromFile( | ||
| '$_aNonExistentFilename.$yamlExtension', | ||
| ), | ||
| throwsArgumentError, | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| group('with an unsupported file extension', () { | ||
| group('when attempting to load any file', () { | ||
| test('then it reports an Unsupported Error', () { | ||
| expect( | ||
| () => ConfigurationParser.fromFile( | ||
| '$_aNonExistentFilename.$_anUnsupportedExtension', | ||
| ), | ||
| throwsUnsupportedError, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.