Skip to content

Commit

Permalink
fix: the package name is now created correctly even when no --path
Browse files Browse the repository at this point in the history
…flag is passed (#151)

* test: added test that works with figmage.yaml

* fix: the package name is now created correctly even when no `--path` flag is passed

* ci: also run full_integration_test in CI
  • Loading branch information
timcreatedit authored Sep 25, 2024
1 parent 094b71f commit fed21d7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/integration_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ jobs:
- name: 🧪 Run Integration Tests
env:
FIGMA_FREE_TOKEN: ${{ secrets.FIGMA_FREE_TOKEN }}
run: dart test integration_test/generation_notifier_integration_test.dart
run: |
dart test integration_test/generation_notifier_integration_test.dart
dart test integration_test/full_integration_test.dart
2 changes: 2 additions & 0 deletions .github/workflows/tag_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
if: contains(github.event.head_commit.message, 'chore(release)')
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: dart-lang/[email protected]
- uses: bluefireteam/melos-action@c7dcb921b23cc520cace360b95d02b37bf09cdaa
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-tags: true
fetch-depth: 0
- uses: dart-lang/[email protected]
- uses: bluefireteam/melos-action@c7dcb921b23cc520cace360b95d02b37bf09cdaa
with:
Expand Down
86 changes: 86 additions & 0 deletions integration_test/full_integration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:convert';
import 'dart:io';

import 'package:test/test.dart';

void main() {
group('Full integration test', () {
setUp(() {});

test('prints help to console', () async {
final process = await Process.start(
'dart',
['run', 'bin/figmage.dart', 'forge', '--help'],
);
final exitCode = await process.exitCode;
expect(exitCode, 0);

const helpText = '''
This command forges a new package from your figma file.
Usage: figmage forge [arguments]
-h, --help Print this usage information.
-t, --token (mandatory) Your figma API token
-p, --path The ouptut path for the generated package, if not provided, the current directory will be used.
-f, --fileId Your figma file ID, needs to be either given here, or in the figmage.yaml
Run "figmage help" to see global options.
''';

await expectLater(
process.stdout.transform(const Utf8Decoder()),
emits(equalsIgnoringWhitespace(helpText)),
);
});

test('generates default package from command', () async {
final dir = Directory('./full_test_package')..createSync();
addTearDown(() => dir.deleteSync(recursive: true));
final process = await Process.start(
'dart',
[
'run',
'../bin/figmage.dart',
'forge',
'-f',
'HHUVTJ7lsjhG24SQB5h0zX',
'-t',
Platform.environment['FIGMA_FREE_TOKEN']!,
],
workingDirectory: dir.path,
);
final exitCode = await process.exitCode;
expect(exitCode, 0);

final pubspec = File("${dir.path}/pubspec.yaml");
final pubspecContent = pubspec.readAsStringSync();
expect(
pubspecContent,
contains("name: full_test_package"),
reason: 'Package name was not generated from directory',
);

final files = [
File("${dir.path}/lib/src/colors.dart"),
File("${dir.path}/lib/src/typography.dart"),
];
expect(
files.every((f) => f.existsSync()),
true,
reason: 'Token files were not generated',
);

final analyzeProcess = await Process.start(
'dart',
['analyze', '--fatal-infos', '--fatal-warnings'],
workingDirectory: dir.path,
);
final analyzeExitCode = await analyzeProcess.exitCode;
expect(
analyzeExitCode,
0,
reason: 'Generated package has analysis issues',
);
});
});
}
12 changes: 6 additions & 6 deletions lib/src/commands/forge/forge_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class ForgeCommand extends Command<int> {
/// {@macro forge_command}
ForgeCommand(this._container) {
argParser
..addOption(
"path",
defaultsTo: ".",
help: "The ouptut path for the generated package, if not provided, "
"the current directory will be used.",
)
..addOption(
"token",
abbr: "t",
help: "Your figma API token",
mandatory: true,
)
..addOption(
"path",
abbr: "p",
help: "The ouptut path for the generated package, if not provided, "
"the current directory will be used.",
)
..addOption(
"fileId",
abbr: "f",
Expand Down
3 changes: 2 additions & 1 deletion lib/src/commands/shared/forge_settings_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import 'package:riverpod/riverpod.dart';
final settingsProvider = FutureProvider.autoDispose
.family<FigmageSettings, ArgResults>((ref, args) async {
final dir = switch (args['path']) {
null => Directory.current,
final String dir => Directory(dir),
_ => throw ArgumentError.notNull('path'),
_ => throw ArgumentError.value(args['path'], 'path'),
};

final configPath = join(dir.path, 'figmage.yaml');
Expand Down
9 changes: 4 additions & 5 deletions test/src/commands/forge/forge_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void main() {

test('tells user how to use command when wrong arguments provided',
() async {
final exitCode = await commandRunner.run(['forge', '-p']);
final exitCode = await commandRunner.run(['forge', '-x']);
verify(
() => logger.err('Could not find an option or flag "-p".'),
() => logger.err('Could not find an option or flag "-x".'),
).called(1);
verify(() => logger.info('')).called(1);
final infoLog = verify(() => logger.info(captureAny())).captured.first;
Expand All @@ -121,17 +121,16 @@ void main() {
final args = command.argParser.parse(['-t', 'token', '-f', 'fileId']);
expect(args['token'], 'token');
expect(args['fileId'], 'fileId');
expect(args['path'], '.', reason: 'defaults to "."');
expect(args['path'], isNull);
});
});
}

const usage = '''
Usage: figmage forge [arguments]
-h, --help Print this usage information.
--path The ouptut path for the generated package, if not provided, the current directory will be used.
(defaults to ".")
-t, --token (mandatory) Your figma API token
-p, --path The ouptut path for the generated package, if not provided, the current directory will be used.
-f, --fileId Your figma file ID, needs to be either given here, or in the figmage.yaml
Run "figmage help" to see global options.''';
4 changes: 2 additions & 2 deletions test/src/commands/shared/forge_settings_providers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ void main() {
expect(result.path, "arg_path");
});

test('throws an ArgumentError if path is not in the args', () async {
when(() => argResults['path']).thenReturn(null);
test('throws an ArgumentError if path is not a string or null', () async {
when(() => argResults['path']).thenReturn(0);
await expectLater(
() => container.read(settingsProvider(argResults).future),
throwsA(
Expand Down

0 comments on commit fed21d7

Please sign in to comment.