Skip to content

Commit 4d2c278

Browse files
committed
implement tests
1 parent 42ce20b commit 4d2c278

File tree

4 files changed

+117
-9
lines changed

4 files changed

+117
-9
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
ckeck:
10+
check:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v4

lib/config_props.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22

33
import 'package:args/args.dart';
4+
import 'package:raygun_cli/environment.dart';
45

56
/// Configuration properties for the Raygun CLI
67
class ConfigProps {
@@ -25,7 +26,7 @@ class ConfigProps {
2526
if (arguments.wasParsed('app-id')) {
2627
appId = arguments['app-id'];
2728
} else {
28-
appId = Platform.environment['RAYGUN_APP_ID'];
29+
appId = Environment.instance.raygunAppId;
2930
}
3031

3132
if (appId == null) {
@@ -38,7 +39,7 @@ class ConfigProps {
3839
if (arguments.wasParsed('token')) {
3940
token = arguments['token'];
4041
} else {
41-
token = Platform.environment['RAYGUN_TOKEN'];
42+
token = Environment.instance.raygunToken;
4243
}
4344

4445
if (token == null) {

lib/environment.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dart:io';
2+
3+
/// Wraps access to Environment variables
4+
/// Allows faking for testing
5+
class Environment {
6+
static String raygunAppIdKey = 'RAYGUN_APP_ID';
7+
static String raygunTokenKey = 'RAYGUN_TOKEN';
8+
9+
final String? raygunAppId;
10+
final String? raygunToken;
11+
12+
static Environment? _instance;
13+
14+
/// Singleton instance access
15+
/// Will init if not already
16+
static Environment get instance {
17+
_instance ??= Environment._init();
18+
return _instance!;
19+
}
20+
21+
/// For testing purposes
22+
static void setInstance(Environment instance) {
23+
_instance = instance;
24+
}
25+
26+
/// Create custom instance
27+
Environment({
28+
required this.raygunAppId,
29+
required this.raygunToken,
30+
});
31+
32+
factory Environment._init() {
33+
final raygunAppId = Platform.environment[raygunAppIdKey];
34+
final raygunToken = Platform.environment[raygunTokenKey];
35+
return Environment(
36+
raygunAppId: raygunAppId,
37+
raygunToken: raygunToken,
38+
);
39+
}
40+
}

test/config_props_test.dart

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,96 @@
1+
import 'dart:io';
2+
13
import 'package:args/args.dart';
24
import 'package:raygun_cli/config_props.dart';
5+
import 'package:raygun_cli/environment.dart';
36
import 'package:test/test.dart';
47

58
void main() {
69
group('ConfigProps', () {
710
test('should parse arguments', () {
811
ArgParser parser = ArgParser()
9-
..addFlag('app-id')
10-
..addFlag('token');
12+
..addFlag('verbose')
13+
..addOption('app-id')
14+
..addOption('token');
1115
final results =
12-
parser.parse(['app-id', 'app-id-parsed', 'token', 'token-parsed']);
16+
parser.parse(['--app-id=app-id-parsed', '--token=token-parsed']);
1317
final props = ConfigProps.load(results);
1418
expect(props.appId, 'app-id-parsed');
1519
expect(props.token, 'token-parsed');
1620
});
1721

1822
test('should parse from env vars', () {
23+
// fake environment variables
24+
Environment.setInstance(
25+
Environment(
26+
raygunAppId: 'app-id-env',
27+
raygunToken: 'token-env',
28+
),
29+
);
30+
31+
// define parser
1932
ArgParser parser = ArgParser()
20-
..addFlag('app-id')
21-
..addFlag('token');
22-
// intentionally empty
33+
..addFlag('verbose')
34+
..addOption('app-id')
35+
..addOption('token');
36+
37+
// parse nothing
2338
final results = parser.parse([]);
39+
40+
// load from env vars
2441
final props = ConfigProps.load(results);
2542
expect(props.appId, 'app-id-env');
2643
expect(props.token, 'token-env');
2744
});
45+
46+
test('should parse with priority', () {
47+
// fake environment variables
48+
Environment.setInstance(
49+
Environment(
50+
raygunAppId: 'app-id-env',
51+
raygunToken: 'token-env',
52+
),
53+
);
54+
55+
// define parser
56+
ArgParser parser = ArgParser()
57+
..addFlag('verbose')
58+
..addOption('app-id')
59+
..addOption('token');
60+
61+
// parse arguments
62+
final results =
63+
parser.parse(['--app-id=app-id-parsed', '--token=token-parsed']);
64+
65+
// load from parsed even if env vars are set
66+
final props = ConfigProps.load(results);
67+
expect(props.appId, 'app-id-parsed');
68+
expect(props.token, 'token-parsed');
69+
});
70+
71+
test('should parse from both', () {
72+
// fake environment variables
73+
// token is not provided
74+
Environment.setInstance(
75+
Environment(
76+
raygunAppId: 'app-id-env',
77+
raygunToken: null,
78+
),
79+
);
80+
81+
// define parser
82+
ArgParser parser = ArgParser()
83+
..addFlag('verbose')
84+
..addOption('app-id')
85+
..addOption('token');
86+
87+
// parse arguments, only token is passed
88+
final results = parser.parse(['--token=token-parsed']);
89+
90+
// app-id from env, token from argument
91+
final props = ConfigProps.load(results);
92+
expect(props.appId, 'app-id-env');
93+
expect(props.token, 'token-parsed');
94+
});
2895
});
2996
}

0 commit comments

Comments
 (0)