Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
m-skolnick committed May 29, 2023
1 parent a2f99a7 commit 4b37cd2
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 6 deletions.
98 changes: 98 additions & 0 deletions test/dependency_parser_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import 'package:changelog_bubbler/src/bubbler_shell.dart';
import 'package:changelog_bubbler/src/dependency_parser.dart';
import 'package:changelog_bubbler/src/global_dependencies.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import 'test_helpers.dart';

void main() {
setUp(() async {
await registerMockGlobalDependencies();
await _prepareTestApp();
});
test(
'includes transitive main dependencies '
'when includeTransitive', () async {
final parser = DependencyParser(repoPath: d.sandbox);

parser.parseDependencies(
includeDev: true,
includeTransitive: true,
);

expect(parser.dependencies.keys, contains('async'));
});

test(
'includes direct dev dependencies '
'when includeDev', () async {
final parser = DependencyParser(repoPath: d.sandbox);

parser.parseDependencies(
includeDev: true,
includeTransitive: true,
);

expect(parser.dependencies.keys, contains('mocktail'));
});
test(
'includes transitive dev dependencies '
'when includeDev and includeTransitive', () async {
final parser = DependencyParser(repoPath: d.sandbox);

parser.parseDependencies(
includeDev: true,
includeTransitive: true,
);

expect(parser.dependencies.keys, contains('matcher'));
});
test(
'excludes direct dev dependencies '
'when not includeDev', () async {
final parser = DependencyParser(repoPath: d.sandbox);

parser.parseDependencies(
includeDev: false,
includeTransitive: true,
);

expect(
parser.dependencies.keys.contains('mocktail'),
isFalse,
);
});
test(
'excludes transitive dev dependencies '
'when includeDev and not includeTransitive', () async {
final parser = DependencyParser(repoPath: d.sandbox);

parser.parseDependencies(
includeDev: true,
includeTransitive: false,
);

expect(
parser.dependencies.keys.contains('matcher'),
isFalse,
);
});
}

Future<void> _prepareTestApp() async {
final pubYaml = d.file('pubspec.yaml');
await pubYaml.create();
pubYaml.io.writeAsStringSync(MockData.mockPubYamlContents);

final pubLock = d.file('pubspec.lock');
await pubLock.create();
pubLock.io.writeAsStringSync(MockData.mockPubLockContents);

final shell = getDep<BubblerShell>();
final mockResult = ProcessResultExt.mock(outText: MockData.mockPubDepsOutput);
when(
() => shell.stubbedRunSync('dart pub deps --no-dev --style compact'),
).thenReturn(mockResult);
}
90 changes: 85 additions & 5 deletions test/diff_builder_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,90 @@
// test added packages
// test removed packages

import 'package:changelog_bubbler/src/bubbler_shell.dart';
import 'package:changelog_bubbler/src/dependency_parser.dart';
import 'package:changelog_bubbler/src/diff_builder.dart';
import 'package:changelog_bubbler/src/global_dependencies.dart';
import 'package:changelog_bubbler/src/package_wrapper.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_lock_parse/pubspec_lock_parse.dart';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import 'test_helpers.dart';

void main() {
test('empty test', () {
expect(2 + 2, 4);
setUp(() async {
await registerMockGlobalDependencies();
await _prepareTestApp();
});
test('creates new section for each host', () async {
final previousParser = DependencyParser(repoPath: d.sandbox)
..dependencies = _previousDependencyMap;
final currentParser = DependencyParser(repoPath: d.sandbox)
..dependencies = _currentDependencyMap;

final diffBuilder = DiffBuilder(
previous: previousParser,
current: currentParser,
changelogName: 'changelogName',
);
expect(await diffBuilder.buildDiff(), contains('async'));
});
}

Future<void> _prepareTestApp() async {
final pubYaml = d.file('pubspec.yaml');
await pubYaml.create();
pubYaml.io.writeAsStringSync(MockData.mockPubYamlContents);

final pubLock = d.file('pubspec.lock');
await pubLock.create();
pubLock.io.writeAsStringSync(MockData.mockPubLockContents);

final shell = getDep<BubblerShell>();
final mockResult = ProcessResultExt.mock(outText: MockData.mockPubDepsOutput);
when(
() => shell.stubbedRunSync('dart pub deps --no-dev --style compact'),
).thenReturn(mockResult);
}

final _previousDependencyMap = {
'hosted': PackageWrapper(
'hosted_dep',
Package(
dependency: 'direct main',
description: HostedPackageDescription(name: 'name', url: 'hosted_url'),
source: PackageSource.hosted,
version: Version.parse('1.0.0'),
),
),
'git_dep': PackageWrapper(
'git_dep',
Package(
dependency: 'direct main',
description: GitPackageDescription(
path: 'path', ref: 'ref', resolvedRef: 'resolvedRef', url: 'git_url'),
source: PackageSource.git,
version: Version.parse('1.0.0'),
),
),
};
final _currentDependencyMap = {
'hosted_dep': PackageWrapper(
'hosted_dep',
Package(
dependency: 'direct main',
description: HostedPackageDescription(name: 'name', url: 'hosted_url'),
source: PackageSource.hosted,
version: Version.parse('2.0.0'),
),
),
'pub_dev_dep': PackageWrapper(
'pub_dev_dep',
Package(
dependency: 'direct dev',
description: HostedPackageDescription(name: 'name', url: 'pub_dev_url'),
source: PackageSource.hosted,
version: Version.parse('2.0.0'),
),
),
};
88 changes: 87 additions & 1 deletion test/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:changelog_bubbler/src/bubbler_shell.dart';
import 'package:changelog_bubbler/src/platform_wrapper.dart';
import 'package:get_it/get_it.dart';
import 'package:mocktail/mocktail.dart';
import 'package:process_run/process_run.dart' show ProcessRunProcessResultExt;

class MockProcessResult extends Mock implements ProcessResult {}

Expand All @@ -24,7 +25,6 @@ void _stubShell() {
any(),
workingDir: any(named: 'workingDir'),
)).thenAnswer((_) async => mockResult);

GetIt.I.registerLazySingleton<BubblerShell>(() => mockShell);
GetIt.I.registerLazySingleton<PlatformWrapper>(() => _MockPlatformWrapper());
}
Expand All @@ -38,4 +38,90 @@ extension TestHelper on BubblerShell {
script,
workingDir: workingDir ?? any(named: 'workingDir'),
);

ProcessResult stubbedRunSync(
String script, {
String? workingDir,
}) =>
runSync(
script,
workingDir: workingDir ?? any(named: 'workingDir'),
);
}

extension ProcessResultExt on ProcessResult {
static ProcessResult mock({String? outText}) {
final mockResult = MockProcessResult();
when(() => mockResult.outText).thenReturn(outText ?? '');
return mockResult;
}
}

class MockData {
/// > dart pub deps --no-dev --style compact
static const mockPubDepsOutput = '''
Dart SDK 2.19.2
Flutter SDK 3.7.3
changelog_bubbler 0.1.0
dependencies:
- get_it 7.6.0 [async]
transitive dependencies:
- async 2.11.0
''';

static const mockPubYamlContents = '''
name: changelog_bubbler
version: 1.0.0
dependencies:
get_it: ^7.4.1
dev_dependencies:
mocktail: ^0.3.0
executables:
changelog_bubbler: changelog_bubbler
''';

static const mockPubLockContents = '''
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
url: "https://pub.dev"
source: hosted
version: "2.11.0"
get_it:
dependency: "direct main"
description:
name: get_it
sha256: "529de303c739fca98cd7ece5fca500d8ff89649f1bb4b4e94fb20954abcd7468"
url: "https://pub.dev"
source: hosted
version: "7.6.0"
matcher:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
url: "https://pub.dev"
source: hosted
version: "0.12.15"
mocktail:
dependency: "direct dev"
description:
name: mocktail
sha256: "80a996cd9a69284b3dc521ce185ffe9150cde69767c2d3a0720147d93c0cef53"
url: "https://pub.dev"
source: hosted
version: "0.3.0"
sdks:
dart: ">=2.19.0 <3.0.0"
''';
}

0 comments on commit 4b37cd2

Please sign in to comment.