Skip to content

Commit 818ff9a

Browse files
committed
wip unit tests.
1 parent 0fabad2 commit 818ff9a

22 files changed

+799
-78
lines changed

.vscode/launch.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@
7474
},
7575

7676
{
77-
"name": "onepub private",
77+
"name": "onepub pub private",
7878
"type": "dart",
7979
"request": "launch",
8080
"program": "bin/onepub.dart",
81-
"args": ["private"],
81+
"args": ["pub", "private"],
8282
"console": "terminal"
8383
},
8484

@@ -120,11 +120,11 @@
120120

121121

122122
{
123-
"name": "onepub private ",
123+
"name": "onepub pub private ",
124124
"type": "dart",
125125
"request": "launch",
126126
"program": "/home/bsutton/git/onepub/onepub/bin/onepub.dart",
127-
"args": ["private"],
127+
"args": ["pub", "private"],
128128
"cwd": "/home/bsutton/git/dvault",
129129
"console": "terminal"
130130
},

a.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SettingsYaml settings file
2+
onepubToken: "5f867e4e-7d69-4673-aa73-1702e826935e"

abc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SettingsYaml settings file
2+
onepubToken: "b1e91051-2df1-4f73-b2eb-2b1d3c4a71d6"

analysis_options.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
2-
#include: package:lint_hard/all.yaml
1+
# include: package:lint_hard/all.yaml

lib/src/commands/export.dart

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class ExportCommand extends Command<int> {
1818
///
1919
ExportCommand() {
2020
argParser
21-
..addFlag('file', abbr: 'f', help: 'Save the OnePub token to a file.')
21+
..addFlag('file', abbr: 'f', defaultsTo: false, help: '''
22+
Save the OnePub token to a file.
23+
Pass in a filename or leave blank to use the default filename.''')
2224
..addOption('user',
2325
abbr: 'u',
24-
help: 'Export the token of a CICD member.',
25-
valueHelp: 'email address of a CI/CD Member');
26+
help: 'Export the token of a CICD member rather than your token.',
27+
valueHelp: 'email address of a CI/CD OnePub Member');
2628
}
2729

2830
@override
@@ -79,18 +81,25 @@ class ExportCommand extends Command<int> {
7981
'Exporting OnePub token for ${OnePubSettings().organisationName}.'));
8082

8183
if (file) {
82-
final exportFile =
83-
TokenExportFile(join(pwd, TokenExportFile.exportFilename))
84-
..onepubToken = onepubToken
85-
..save();
84+
var pathToFile = TokenExportFile.exportFilename;
85+
if (argResults!.rest.length == 1) {
86+
pathToFile = argResults!.rest[0];
87+
} else if (argResults!.rest.length != 0) {
88+
throw ExitException(
89+
exitCode: 1, message: 'You may only pass one argument to --file.');
90+
}
91+
92+
final exportFile = TokenExportFile(pathToFile)
93+
..onepubToken = onepubToken
94+
..save();
8695

8796
print('''
8897
89-
Saved credentials to: ${truepath(exportFile.pathToExportFile)}.
98+
Saved OnePub token to: ${truepath(exportFile.pathToExportFile)}.
9099
91-
Copy the onepub.token.yaml to your CI/CD environment and run:
100+
Copy the ${exportFile.pathToExportFile} to your CI/CD environment and run:
92101
93-
onepub import <path to credentials>
102+
onepub import <path to OnePub token file>
94103
95104
''');
96105
} else {

lib/src/commands/import.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ Use `onepub export` to obtain the OnePub token.
5353
///
5454
Future<void> import() async {
5555
OnePubSettings.load();
56+
if (OnePubTokenStore().isLoggedIn) {
57+
throw ExitException(
58+
exitCode: -1,
59+
message:
60+
'You may not import a token when you are logged in to the OnePub CLI.');
61+
}
5662
final file = argResults!['file'] as bool;
5763
final ask = argResults!['ask'] as bool;
5864

@@ -112,6 +118,12 @@ Found: ${argResults!.rest.join(',')}''');
112118
pathToTokenFile = argResults!.rest[0];
113119
}
114120

121+
if (!exists(pathToTokenFile)) {
122+
throw ExitException(
123+
exitCode: 1,
124+
message: "The token file '$pathToTokenFile', does not exist");
125+
}
126+
115127
return TokenExportFile.load(pathToTokenFile).onepubToken;
116128
}
117129

lib/src/commands/pub.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import 'package:args/command_runner.dart';
77
import 'package:dcli/dcli.dart';
88

9-
import 'pub/add_private.dart';
9+
import 'pub/add_dep.dart';
1010
import 'pub/global.dart';
1111
import 'pub/private.dart';
1212

lib/src/commands/pub/add_dep.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2020, the Dart project authors and OnePub IP Pty Ltd
2+
//
3+
// Please see the AUTHORS file
4+
// for details. All rights reserved. Use of this source code is governed by a
5+
// BSD-style license that can be found in the LICENSE file.
6+
7+
import 'package:dcli/dcli.dart';
8+
import 'package:onepub/src/pub/command/add.dart';
9+
10+
import '../../exceptions.dart';
11+
import '../../onepub_settings.dart';
12+
import '../../util/one_pub_token_store.dart';
13+
14+
/// Handles the `add` pub command. Adds a dependency to `pubspec.yaml` and gets
15+
/// the package. The user may pass in a git constraint, host url, or path as
16+
/// requirements. If no such options are passed in, this command will do a
17+
/// resolution to find the latest version of the package that is compatible with
18+
/// the other dependencies in `pubspec.yaml`, and then enter that as the lower
19+
/// bound in a ^x.y.z constraint.
20+
///
21+
/// Currently supports only adding one dependency at a time.
22+
class AddPrivateCommand extends AddCommand {
23+
@override
24+
String get name => 'add';
25+
@override
26+
String get description => blue('Add private dependencies to pubspec.yaml.');
27+
@override
28+
String get argumentsDescription =>
29+
'<package>[:<constraint>] [<package2>[:<constraint2>]...] [options]';
30+
@override
31+
String get docUrl => 'https://dart.dev/tools/pub/cmd/pub-add';
32+
33+
@override
34+
String? get hostUrl {
35+
final url = OnePubSettings().onepubHostedUrl().toString();
36+
print(url);
37+
return url;
38+
}
39+
40+
@override
41+
bool get isOffline => false;
42+
43+
bool get hasHostOptions => hostUrl != null;
44+
45+
AddPrivateCommand() : super(includeSourceOptions: false);
46+
47+
@override
48+
Future<void> runProtected() async {
49+
OnePubSettings.load();
50+
51+
if (!OnePubTokenStore().isLoggedIn) {
52+
throw ExitException(
53+
exitCode: 1, message: "You must run 'onepub login' first.");
54+
}
55+
await super.runProtected();
56+
}
57+
}

lib/src/commands/pub/private.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import 'dart:io';
77

88
import 'package:args/command_runner.dart';
9-
import 'package:dcli/dcli.dart';
9+
import 'package:dcli/dcli.dart' hide PubSpec;
10+
import 'package:pubspec2/pubspec.dart';
11+
import 'package:scope/scope.dart';
1012
import 'package:url_builder/url_builder.dart';
1113

14+
import '../../entry_point.dart';
1215
import '../../exceptions.dart';
1316
import '../../onepub_paths.dart';
1417
import '../../onepub_settings.dart';
@@ -18,7 +21,10 @@ import '../../util/send_command.dart';
1821
///
1922
class PrivateCommand extends Command<int> {
2023
///
21-
PrivateCommand();
24+
PrivateCommand() {}
25+
26+
@override
27+
bool get takesArguments => false;
2228

2329
@override
2430
String get description => '''
@@ -43,7 +49,8 @@ See ${urlJoin(OnePubSettings().onepubWebUrl, 'publish')}''';
4349
exitCode: 1, message: "You must run 'onepub login' first.");
4450
}
4551

46-
final project = DartProject.findProject(pwd);
52+
final workingDirectory = Scope.use(unitTestWorkingDirectoryKey);
53+
final project = DartProject.findProject(workingDirectory);
4754
if (project == null) {
4855
throw ExitException(
4956
exitCode: 1,
@@ -59,7 +66,7 @@ See ${urlJoin(OnePubSettings().onepubWebUrl, 'publish')}''';
5966
final currentOrganisationName = OnePubSettings().organisationName;
6067
final url = OnePubSettings().onepubHostedUrl().toString();
6168

62-
final pubspec = project.pubSpec.pubspec;
69+
final pubspec = await PubSpec.loadFile(project.pathToPubSpec);
6370
if (pubspec.publishTo != null) {
6471
if (pubspec.publishTo.toString() == url) {
6572
print(orange('${pubspec.name} is already a private package.'));

lib/src/entry_point.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io';
44

55
import 'package:args/command_runner.dart';
66
import 'package:dcli/dcli.dart';
7+
import 'package:scope/scope.dart';
78

89
import 'exceptions.dart';
910
import 'my_runner.dart';
@@ -14,8 +15,16 @@ import 'util/log.dart' as ulog;
1415
* Written by Brett Sutton <[email protected]>, Jan 2022
1516
*/
1617

18+
/// Used by unit tests to alter the working directory a command runs in
19+
/// as onepub normally assumes it is operating in the pwd.
20+
ScopeKey<String> unitTestWorkingDirectoryKey =
21+
ScopeKey<String>.withDefault(pwd, "WorkingDirectory");
22+
1723
Future<void> entrypoint(
18-
List<String> args, CommandSet commandSet, String program) async {
24+
List<String> args,
25+
CommandSet commandSet,
26+
String program,
27+
) async {
1928
try {
2029
MyRunner runner = MyRunner(args, program, _description, commandSet);
2130
try {

0 commit comments

Comments
 (0)