Skip to content

Commit

Permalink
update lints (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Nov 7, 2022
1 parent 66f14ce commit 13dbc20
Show file tree
Hide file tree
Showing 18 changed files with 531 additions and 347 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [2.12.0, dev]
sdk: [2.17.0, dev]
steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
- uses: dart-lang/setup-dart@6a218f2413a3e78e9087f638a238f6b40893203d
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 2.0.2-dev

## 2.0.1

* Populate the pubspec `repository` field.
Expand Down
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@ define a filesystem structure that can be created using
```dart
import 'dart:io';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
void main() {
test("Directory.rename", () async {
await d.dir("parent", [
d.file("sibling", "sibling-contents"),
d.dir("old-name", [
d.file("child", "child-contents")
])
test('Directory.rename', () async {
await d.dir('parent', [
d.file('sibling', 'sibling-contents'),
d.dir('old-name', [d.file('child', 'child-contents')])
]).create();
await new Directory("${d.sandbox}/parent/old-name")
.rename("${d.sandbox}/parent/new-name");
await Directory('${d.sandbox}/parent/old-name')
.rename('${d.sandbox}/parent/new-name');
await d.dir("parent", [
d.file("sibling", "sibling-contents"),
d.dir("new-name", [
d.file("child", "child-contents")
])
await d.dir('parent', [
d.file('sibling', 'sibling-contents'),
d.dir('new-name', [d.file('child', 'child-contents')])
]).validate();
});
}
Expand Down
75 changes: 35 additions & 40 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
# https://dart.dev/guides/language/analysis-options
include: package:lints/recommended.yaml

analyzer:
strong-mode:
implicit-casts: false
language:
strict-casts: true
strict-inference: true
strict-raw-types: true

linter:
rules:
- annotate_overrides
- always_declare_return_types
- avoid_bool_literals_in_conditional_expressions
- avoid_catching_errors
- avoid_classes_with_only_static_members
- avoid_dynamic_calls
- avoid_function_literals_in_foreach_calls
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_relative_lib_imports
- avoid_private_typedef_functions
- avoid_redundant_argument_values
- avoid_returning_null
- avoid_returning_null_for_future
- avoid_returning_this
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
- avoid_void_async
- cancel_subscriptions
- comment_references
- constant_identifier_names
- control_flow_in_finally
- directives_ordering
- empty_catches
- empty_constructor_bodies
- empty_statements
- hash_and_equals
- implementation_imports
- iterable_contains_unrelated_type
- library_names
- library_prefixes
- list_remove_unrelated_type
- join_return_with_assignment
- lines_longer_than_80_chars
- literal_only_boolean_expressions
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- non_constant_identifier_names
- no_runtimeType_toString
- omit_local_variable_types
- only_throw_errors
- overridden_fields
- package_api_docs
- package_names
- package_prefixed_library_names
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_asserts_in_initializer_lists
- prefer_const_constructors
- prefer_final_fields
- prefer_generic_function_type_aliases
- prefer_initializing_formals
- prefer_interpolation_to_compose_strings
- prefer_const_declarations
- prefer_expression_function_bodies
- prefer_final_locals
- prefer_relative_imports
- prefer_single_quotes
- prefer_typing_uninitialized_variables
- slash_for_doc_comments
- sort_pub_dependencies
- test_types_in_equals
- throw_in_finally
- type_init_formals
- unnecessary_brace_in_string_interps
- unnecessary_const
- unnecessary_getters_setters
- type_annotate_public_apis
- unawaited_futures
- unnecessary_await_in_return
- unnecessary_lambdas
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_parenthesis
- unnecessary_raw_strings
- unnecessary_statements
- unnecessary_this
- use_if_null_to_convert_nulls_to_bools
- use_raw_strings
- use_string_buffers
- use_super_parameters
- require_trailing_commas
25 changes: 25 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

void main() {
test('Directory.rename', () async {
await d.dir('parent', [
d.file('sibling', 'sibling-contents'),
d.dir('old-name', [d.file('child', 'child-contents')])
]).create();

await Directory('${d.sandbox}/parent/old-name')
.rename('${d.sandbox}/parent/new-name');

await d.dir('parent', [
d.file('sibling', 'sibling-contents'),
d.dir('new-name', [d.file('child', 'child-contents')])
]).validate();
});
}
90 changes: 53 additions & 37 deletions lib/src/directory_descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,51 @@ class DirectoryDescriptor extends Descriptor {
/// [sandbox].
Directory get io => Directory(p.join(sandbox, name));

DirectoryDescriptor(String name, Iterable<Descriptor> contents)
: contents = contents.toList(),
super(name);
DirectoryDescriptor(super.name, Iterable<Descriptor> contents)
: contents = contents.toList();

/// Creates a directory descriptor named [name] that describes the physical
/// directory at [path].
factory DirectoryDescriptor.fromFilesystem(String name, String path) {
return DirectoryDescriptor(
factory DirectoryDescriptor.fromFilesystem(String name, String path) =>
DirectoryDescriptor(
name,
Directory(path).listSync().map((entity) {
// Ignore hidden files.
if (p.basename(entity.path).startsWith('.')) return null;

if (entity is Directory) {
return DirectoryDescriptor.fromFilesystem(
p.basename(entity.path), entity.path);
p.basename(entity.path),
entity.path,
);
} else if (entity is File) {
return FileDescriptor(
p.basename(entity.path), entity.readAsBytesSync());
p.basename(entity.path),
entity.readAsBytesSync(),
);
}
// Ignore broken symlinks.
return null;
}).whereType<Descriptor>());
}
}).whereType<Descriptor>(),
);

@override
Future<void> create([String? parent]) async {
var fullPath = p.join(parent ?? sandbox, name);
final fullPath = p.join(parent ?? sandbox, name);
await Directory(fullPath).create(recursive: true);
await Future.wait(contents.map((entry) => entry.create(fullPath)));
}

@override
Future<void> validate([String? parent]) async {
var fullPath = p.join(parent ?? sandbox, name);
final fullPath = p.join(parent ?? sandbox, name);
if (!(await Directory(fullPath).exists())) {
fail('Directory not found: "${prettyPath(fullPath)}".');
}

await waitAndReportErrors(
contents.map((entry) => entry.validate(fullPath)));
contents.map((entry) => entry.validate(fullPath)),
);
}

/// Treats this descriptor as a virtual filesystem and loads the binary
Expand All @@ -80,35 +84,47 @@ class DirectoryDescriptor extends Descriptor {
Stream<List<int>> _load(String path, [String? parents]) {
if (!p.url.isWithin('.', path)) {
throw ArgumentError.value(
path, 'path', 'must be relative and beneath the base URL.');
path,
'path',
'must be relative and beneath the base URL.',
);
}

return StreamCompleter.fromFuture(Future.sync(() {
var split = p.url.split(p.url.normalize(path));
var file = split.length == 1;
var matchingEntries = contents.where((entry) {
return entry.name == split.first &&
(file ? entry is FileDescriptor : entry is DirectoryDescriptor);
}).toList();

var type = file ? 'file' : 'directory';
var parentsAndSelf = parents == null ? name : p.url.join(parents, name);
if (matchingEntries.isEmpty) {
fail('Couldn\'t find a $type descriptor named "${split.first}" within '
'"$parentsAndSelf".');
} else if (matchingEntries.length > 1) {
fail('Found multiple $type descriptors named "${split.first}" within '
'"$parentsAndSelf".');
} else {
var remainingPath = split.sublist(1);
if (remainingPath.isEmpty) {
return (matchingEntries.first as FileDescriptor).readAsBytes();
return StreamCompleter.fromFuture(
Future.sync(() {
final split = p.url.split(p.url.normalize(path));
final file = split.length == 1;
final matchingEntries = contents
.where(
(entry) =>
entry.name == split.first &&
(file
? entry is FileDescriptor
: entry is DirectoryDescriptor),
)
.toList();

final type = file ? 'file' : 'directory';
final parentsAndSelf =
parents == null ? name : p.url.join(parents, name);
if (matchingEntries.isEmpty) {
fail(
'Couldn\'t find a $type descriptor named "${split.first}" within '
'"$parentsAndSelf".');
} else if (matchingEntries.length > 1) {
fail('Found multiple $type descriptors named "${split.first}" within '
'"$parentsAndSelf".');
} else {
return (matchingEntries.first as DirectoryDescriptor)
._load(p.url.joinAll(remainingPath), parentsAndSelf);
final remainingPath = split.sublist(1);
if (remainingPath.isEmpty) {
return (matchingEntries.first as FileDescriptor).readAsBytes();
} else {
return (matchingEntries.first as DirectoryDescriptor)
._load(p.url.joinAll(remainingPath), parentsAndSelf);
}
}
}
}));
}),
);
}

@override
Expand Down
Loading

0 comments on commit 13dbc20

Please sign in to comment.