Skip to content
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
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI

on:
push:
branches:
- master
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
cache: true
- name: Install dependencies
run: flutter pub get
- name: Analyze
run: dart analyze bin lib test
- name: Check formatting
run: dart format --output=none --set-exit-if-changed bin lib test
- name: Run tests
run: dart test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.1

* Always end the generated `pubspec.yaml` section with a final newline.

## 5.0.0

* Support `package:analyzer` v12 and v13.
Expand Down
2 changes: 1 addition & 1 deletion example/module_a/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ flutter:
# assets end


# assets end
# assets end
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ flutter:
- assets/images/flutter_candies_logo.png
- assets/test.txt

# assets end
# assets end
4 changes: 1 addition & 3 deletions lib/src/yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ class Yaml {
// yamlString = yamlString.replaceAll('assets:', '').trim();
// }

yamlString = yamlString.trim();

yamlFile.writeAsStringSync(yamlString);
yamlFile.writeAsStringSync('${yamlString.trim()}\n');
print(green.wrap('${yamlFile.path} is changed automatically.'));
}
}
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: assets_generator
description: The flutter tool to generate assets‘s configs(yaml) and consts automatically for single project and multiple modules.
version: 5.0.0
version: 5.0.1
homepage: https://github.com/fluttercandies/assets_generator

environment:
Expand All @@ -21,6 +21,7 @@ dependencies:

dev_dependencies:
lints: any
test: ^1.25.0

executables:
agen: assets_generator
Expand Down
154 changes: 154 additions & 0 deletions test/yaml_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'dart:io';

import 'package:assets_generator/src/arg/type.dart';
import 'package:assets_generator/src/yaml.dart';
import 'package:test/test.dart';

void main() {
late Directory tmp;

setUp(() {
tmp = Directory.systemTemp.createTempSync('assets_generator_test');
});

tearDown(() {
tmp.deleteSync(recursive: true);
});

File pubspec(String content) =>
File('${tmp.path}${Platform.pathSeparator}pubspec.yaml')
..writeAsStringSync(content);

/// Runs the yaml writer [times] times the same way the generator does
/// (fresh Yaml per run, freshly scanned asset list) and returns the file
/// content after every run.
List<String> generate(File file, List<String> assets, int times) {
final List<String> snapshots = <String>[];
for (int i = 0; i < times; i++) {
Yaml(file, assets.toList(), <String>[], FormatType.file).write();
snapshots.add(file.readAsStringSync());
}
return snapshots;
}

void expectConverged(List<String> snapshots) {
for (int i = 1; i < snapshots.length; i++) {
expect(
snapshots[i],
snapshots.first,
reason: 'run ${i + 1} differs from run 1',
);
}
}

test('block present with content below it: repeated runs are stable', () {
final File file = pubspec('''
name: sample

dependencies:
flutter:
sdk: flutter

flutter:
uses-material-design: true

assets:
# assets start

# GENERATED CODE - DO NOT MODIFY MANUALLY
# **************************************************************************
# Auto generated by https://github.com/fluttercandies/assets_generator
# **************************************************************************

- assets/a.txt

# assets end

# manual comment below the write region
fonts:
- family: Foo
fonts:
- asset: assets/fonts/Foo.ttf
''');

final List<String> snapshots = generate(
file,
<String>['assets/a.txt', 'assets/images/logo.png'],
5,
);

expectConverged(snapshots);
expect(snapshots.last, contains('# manual comment below the write region'));
expect(snapshots.last, contains('fonts:'));
expect(snapshots.last, contains('- family: Foo'));
expect(snapshots.last, endsWith('\n'));
});

test(
'inserting into an empty assets key keeps sibling content below intact',
() {
final File file = pubspec('''
name: sample

flutter:
assets:
uses-material-design: true
''');

final List<String> snapshots = generate(
file,
<String>['assets/images/logo.png'],
5,
);

expectConverged(snapshots);
expect(snapshots.first, contains('# assets start'));
expect(snapshots.first, contains('uses-material-design: true'));
final int blockStart = snapshots.first.indexOf('# assets start');
final int below = snapshots.first.indexOf('uses-material-design: true');
expect(below, greaterThan(blockStart));
},
);

test('inserting into flutter keeps top-level keys below it intact', () {
final File file = pubspec('''
name: sample

flutter:
uses-material-design: true

custom_fonts:
myfont: value
''');

final List<String> snapshots = generate(
file,
<String>['assets/images/logo.png'],
5,
);

expectConverged(snapshots);
expect(snapshots.first, contains('# assets start'));
expect(snapshots.first, contains('custom_fonts:'));
expect(snapshots.first, contains('myfont: value'));
expect(snapshots.first, endsWith('\n'));
});

test('block at end of file: repeated runs keep the final newline', () {
final File file = pubspec('''
name: sample

flutter:
uses-material-design: true
''');

final List<String> snapshots = generate(
file,
<String>['assets/a.txt'],
5,
);

expectConverged(snapshots);
expect(snapshots.first, endsWith('# assets end\n'));
});
}
Loading