Skip to content
Open
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
49 changes: 49 additions & 0 deletions packages/stac_cli/test/commands/cli_commands_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:test/test.dart';
import 'package:args/command_runner.dart';
import 'package:stac_cli/src/commands/build_command.dart';
import 'package:stac_cli/src/commands/init_command.dart';
import 'package:stac_cli/src/commands/deploy_command.dart';
import 'package:stac_cli/src/config/env.dart';

/// Test suite for verifying core Stac CLI commands.
void main() {
group('CLI Commands', () {
late CommandRunner<int> runner;

setUp(() {
// Initialize environment with mock values to satisfy required checks in services.
// This allows testing command configuration without needing real API keys.
configureEnvironment({
'STAC_BASE_API_URL': 'https://api.test.stac.dev',
'STAC_GOOGLE_CLIENT_ID': 'test-client-id',
'STAC_FIREBASE_API_KEY': 'test-api-key',
});

runner = CommandRunner<int>('stac', 'Stac CLI test runner');
runner.addCommand(BuildCommand());
runner.addCommand(InitCommand());
runner.addCommand(DeployCommand());
});
Comment on lines +13 to +26
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add tearDown to reset environment configuration.

The setUp configures environment variables before each test, but there's no corresponding tearDown to clean up. This could cause state pollution if other test groups or subsequent test runs expect a clean environment.

🧹 Proposed fix to add tearDown
     runner.addCommand(DeployCommand());
   });
+
+  tearDown(() {
+    // Reset environment to prevent state pollution between tests
+    configureEnvironment({});
+  });

   test('build command has correct name and description', () {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
setUp(() {
// Initialize environment with mock values to satisfy required checks in services.
// This allows testing command configuration without needing real API keys.
configureEnvironment({
'STAC_BASE_API_URL': 'https://api.test.stac.dev',
'STAC_GOOGLE_CLIENT_ID': 'test-client-id',
'STAC_FIREBASE_API_KEY': 'test-api-key',
});
runner = CommandRunner<int>('stac', 'Stac CLI test runner');
runner.addCommand(BuildCommand());
runner.addCommand(InitCommand());
runner.addCommand(DeployCommand());
});
setUp(() {
// Initialize environment with mock values to satisfy required checks in services.
// This allows testing command configuration without needing real API keys.
configureEnvironment({
'STAC_BASE_API_URL': 'https://api.test.stac.dev',
'STAC_GOOGLE_CLIENT_ID': 'test-client-id',
'STAC_FIREBASE_API_KEY': 'test-api-key',
});
runner = CommandRunner<int>('stac', 'Stac CLI test runner');
runner.addCommand(BuildCommand());
runner.addCommand(InitCommand());
runner.addCommand(DeployCommand());
});
tearDown(() {
// Reset environment to prevent state pollution between tests
configureEnvironment({});
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/stac_cli/test/commands/cli_commands_test.dart` around lines 13 - 26,
The tests set environment variables in the setUp using configureEnvironment but
never clear them, so add a tearDown block that resets or clears those keys after
each test; implement tearDown(() { configureEnvironment({'STAC_BASE_API_URL':
null, 'STAC_GOOGLE_CLIENT_ID': null, 'STAC_FIREBASE_API_KEY': null}); }) or call
your existing environment-reset helper to remove those variables so
configureEnvironment and setUp no longer leak state between tests.


test('build command has correct name and description', () {
final command = runner.commands['build'];
expect(command, isNotNull, reason: 'BuildCommand should be registered');
expect(command!.name, equals('build'));
expect(command.description, isNotEmpty);
});

test('init command has correct name and description', () {
final command = runner.commands['init'];
expect(command, isNotNull, reason: 'InitCommand should be registered');
expect(command!.name, equals('init'));
expect(command.description, isNotEmpty);
});

test('deploy command has correct name and description', () {
final command = runner.commands['deploy'];
expect(command, isNotNull, reason: 'DeployCommand should be registered');
expect(command!.name, equals('deploy'));
expect(command.description, isNotEmpty);
});
});
}
49 changes: 49 additions & 0 deletions packages/stac_cli/test/utils/file_utils_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:stac_cli/src/utils/file_utils.dart';
import 'package:path/path.dart' as path;

/// Test suite for Stac CLI file utility operations.
void main() {
group('FileUtils', () {
// Basic verification of environment-dependent directory getters.
test('homeDirectory returns a non-empty string on this OS', () {
final home = FileUtils.homeDirectory;
expect(home, isNotEmpty);
});

test('configDirectory path is generated', () {
final config = FileUtils.configDirectory;
expect(config, isNotEmpty);
});

// Integrated test for file system operations using a temporary directory.
test('integrated file operations: create, read, and delete', () async {
// Setup a clean temporary sandbox for this test.
final tempDir = Directory.systemTemp.createTempSync('stac_cli_test');
final filePath = path.join(tempDir.path, 'test_file.txt');

try {
// 1. Initial State: file should not exist.
expect(await FileUtils.fileExists(filePath), isFalse);

// 2. Write Operation: create file with content.
await FileUtils.writeFile(filePath, 'hello world');
expect(await FileUtils.fileExists(filePath), isTrue);

// 3. Read Operation: verify content matches.
final content = await FileUtils.readFile(filePath);
expect(content, equals('hello world'));

// 4. Delete Operation: cleanup file.
await FileUtils.deleteFile(filePath);
expect(await FileUtils.fileExists(filePath), isFalse);
} finally {
// Always cleanup the temporary directory logic even if tests fail.
if (tempDir.existsSync()) {
tempDir.deleteSync(recursive: true);
}
}
});
});
}