Skip to content

Commit

Permalink
fix: The onlyApplicableTo parameter in Robots.parse() not being t…
Browse files Browse the repository at this point in the history
…aken into account.
  • Loading branch information
vxern committed Jan 8, 2023
1 parent 5f94cb0 commit 1da130b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.1

- Converted the `onlyApplicableTo` parameter in `Robots.parse()` from a `String`
into a `Set` to allow multiple user-agents to be specified at once.
- Fixed the `onlyApplicableTo` parameter in `Robots.parse()` not being taken
into account.

## 2.0.0

- Additions:
Expand Down
14 changes: 9 additions & 5 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class Robots {
/// rulesets that do not apply to it.
///
/// This function will never throw an exception.
factory Robots.parse(String contents, {String? onlyApplicableTo}) {
factory Robots.parse(
String contents, {
Set<String>? onlyApplicableTo,
}) {
contents = contents.replaceAll(commentPattern, '');

if (contents.trim().isEmpty) {
Expand All @@ -38,14 +41,14 @@ class Robots {

final lines = contents.split('\n').where((line) => line.isNotEmpty);

return Robots._fromLines(lines, onlyFor: onlyApplicableTo);
return Robots._fromLines(lines, onlyApplicableTo: onlyApplicableTo);
}

/// Iterates over [lines] and sequentially parses each ruleset, optionally
/// ignoring those rulesets which are not relevant to [onlyFor].
/// ignoring those rulesets which are not relevant to [onlyApplicableTo].
factory Robots._fromLines(
Iterable<String> lines, {
String? onlyFor,
Set<String>? onlyApplicableTo,
}) {
final rulesets = <Ruleset>[];
final sitemaps = <String>[];
Expand Down Expand Up @@ -94,7 +97,8 @@ class Robots {
reset();
}

if (onlyFor != null && field.key != onlyFor) {
if (onlyApplicableTo != null &&
!onlyApplicableTo.contains(field.value)) {
break;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: robots_txt
version: 2.0.0
version: 2.0.1

description: A complete, dependency-less and fully documented `robots.txt` ruleset parser.

Expand Down
8 changes: 8 additions & 0 deletions test/contents_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ ${FieldType.userAgent.toField('A')}
${FieldType.allow.toField('/file.txt')}
''';

/// File disallowed for 'A', 'B' and 'C'.
final fileDisallowedForAAndBAndC = '''
${FieldType.userAgent.toField('A')}
${FieldType.userAgent.toField('B')}
${FieldType.userAgent.toField('C')}
${FieldType.disallow.toField('/file.txt')}
''';

/// Directory disallowed.
final directoryDisallowed = '''
${FieldType.userAgent.toField('*')}
Expand Down
31 changes: 31 additions & 0 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'contents_definitions.dart';

void main() {
late Robots robots;

group('The parser correctly parses', () {
group('file contents', () {
test('that are empty.', () {
Expand Down Expand Up @@ -131,6 +132,36 @@ void main() {
equals(false),
);
});

test(
'that disallow a file for A, B and C, but the parser only reads '
'rules applicable to A and C.',
() {
expect(
() => robots = Robots.parse(
fileDisallowedForAAndBAndC,
onlyApplicableTo: const {'A', 'C'},
),
returnsNormally,
);
expect(
robots.rulesets.map((ruleset) => ruleset.userAgent).toSet(),
equals(<String>{'A', 'C'}),
);
expect(
robots.verifyCanAccess('/file.txt', userAgent: 'A'),
equals(false),
);
expect(
robots.verifyCanAccess('/file.txt', userAgent: 'B'),
equals(true),
);
expect(
robots.verifyCanAccess('/file.txt', userAgent: 'C'),
equals(false),
);
},
);
});

group('rules', () {
Expand Down

0 comments on commit 1da130b

Please sign in to comment.