Skip to content

Commit e31f2c8

Browse files
committed
Validate if current path is a Git Repository
1 parent 1ec7cc2 commit e31f2c8

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

lib/constants/constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const String GIT_REMOTE_HARD_RESET = '''
1515
git reset --hard origin/{1}
1616
''';
1717

18+
const String GIT_TOP_LEVEL_PATH = "git rev-parse --show-toplevel";
19+
1820
const GIT_SSL_VERIFY_FALSE = "http.sslVerify=false";
1921

2022
const TXT_FILENAME = "properties_file.txt";

lib/exceptions/exceptions.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ class ExecutableNotFoundInPathException extends AppException {
1717
class ConfigPropertyMissingException extends AppException {
1818
const ConfigPropertyMissingException({
1919
required final String property,
20-
}) : super('The config property "$property" is not defined');
20+
}) : super('"$property" property is not defined int the .env file');
2121
}
2222

23-
abstract class YamlException extends AppException {
24-
YamlException(super._message);
23+
class InvalidValidGitPathException extends AppException {
24+
const InvalidValidGitPathException({
25+
required final String path
26+
}) : super('"$path" path is and invalid git repository');
27+
}
28+
29+
class IncorrectTopLevelGitPathException extends AppException {
30+
const IncorrectTopLevelGitPathException({
31+
required final String path
32+
}) : super('"$path" path is not in the top level of the git repository');
2533
}

lib/services/repo_service.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import 'package:process_run/process_run.dart';
33
import '../config/app_config.dart';
44
import '../constants/constants.dart' as constants;
55
import '../config/logger.dart';
6+
import '../exceptions/exceptions.dart';
67
import '../utils/string_utils.dart';
78
import 'shell_service.dart';
9+
import 'package:path/path.dart' as path;
810

911
class RepoService {
1012
final AppConfig _appConfig;
@@ -36,14 +38,28 @@ class RepoService {
3638
);
3739
}
3840

41+
Future<void> _validateCurrentPathIsGitRepo() async {
42+
try {
43+
final res = await _shellService.runScript(constants.GIT_TOP_LEVEL_PATH);
44+
if (!path.equals(_appConfig.gitRepoPath, res.outLines.first)) {
45+
throw IncorrectTopLevelGitPathException(
46+
path: _appConfig.gitRepoPath,
47+
);
48+
}
49+
} on ShellException {
50+
throw InvalidValidGitPathException(
51+
path: _appConfig.gitRepoPath,
52+
);
53+
}
54+
}
55+
3956
Future<void> setup() async {
4057
log.i("Checking properties for: ${_appConfig.gitRepoPath}");
4158
_shellService.moveShellTo(_appConfig.gitRepoPath);
4259

43-
log.i("Checking if {} is installed".format([constants.GIT]));
4460
_shellService.checkExecutable(constants.GIT);
4561

46-
//TODO: Validate if is a git folder
62+
await _validateCurrentPathIsGitRepo();
4763

4864
if (_appConfig.gitBranch == "") {
4965
log.w("You don't define an specific branch, using current branch");

test/services/repo_service_test.dart

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import 'package:config_props_extractor/constants/constants.dart';
44
import 'package:config_props_extractor/exceptions/exceptions.dart';
55
import 'package:config_props_extractor/services/repo_service.dart';
66
import 'package:config_props_extractor/services/shell_service.dart';
7+
import 'package:config_props_extractor/utils/string_utils.dart';
78
import 'package:mocktail/mocktail.dart';
9+
import 'package:process_run/process_run.dart';
810
import 'package:test/test.dart';
911

1012
import '../helpers/mocks.dart';
@@ -50,9 +52,18 @@ void main() {
5052
);
5153
when(() => mockAppConfig.gitForceRemote).thenReturn(gitForceRemote);
5254
when(() => mockAppConfig.gitSSLEnabled).thenReturn(true);
53-
when(() => mockShellService.runScript(any())).thenAnswer(
55+
when(() => mockShellService.runScript(GIT_BRANCH_SHOW_CURRENT)).thenAnswer(
5456
(_) async => [ProcessResult(pid, exitCode, branch, stderr)],
5557
);
58+
when(() => mockShellService.runScript(GIT_CHECKOUT.format([branch]))).thenAnswer(
59+
(_) async => [],
60+
);
61+
when(() => mockShellService.runScript(GIT_REMOTE_HARD_RESET.format([branch, ""]))).thenAnswer(
62+
(_) async => [],
63+
);
64+
when(() => mockShellService.runScript(GIT_TOP_LEVEL_PATH)).thenAnswer(
65+
(_) async => [ProcessResult(pid, exitCode, path, stderr)],
66+
);
5667

5768
// act
5869
await repoService.setup();
@@ -70,7 +81,7 @@ void main() {
7081
() async => await testSetup(
7182
isBranchDefined: true,
7283
gitForceRemote: true,
73-
runScriptTimes: 1,
84+
runScriptTimes: 2,
7485
),
7586
);
7687

@@ -79,7 +90,7 @@ void main() {
7990
() async => testSetup(
8091
isBranchDefined: true,
8192
gitForceRemote: true,
82-
runScriptTimes: 1,
93+
runScriptTimes: 2,
8394
),
8495
);
8596

@@ -88,7 +99,7 @@ void main() {
8899
() async => testSetup(
89100
isBranchDefined: false,
90101
gitForceRemote: false,
91-
runScriptTimes: 2,
102+
runScriptTimes: 3,
92103
),
93104
);
94105

@@ -123,6 +134,36 @@ void main() {
123134
throwsA(isA<ExecutableNotFoundInPathException>()),
124135
);
125136
});
137+
138+
139+
test("The path is invalid git repository", () {
140+
// arrange
141+
when(() => mockAppConfig.gitRepoPath).thenReturn("/path/to/sub/folder");
142+
when(() => mockShellService.runScript(GIT_TOP_LEVEL_PATH)).thenAnswer(
143+
(_) async => [ProcessResult(pid, exitCode, "/path/to/sub", stderr)],
144+
);
145+
146+
expect(
147+
// act
148+
() => repoService.setup(),
149+
// assert
150+
throwsA(isA<IncorrectTopLevelGitPathException>()),
151+
);
152+
});
153+
154+
test("The path is not in the top-level of the git repository", () {
155+
// arrange
156+
when(() => mockAppConfig.gitRepoPath).thenReturn("/path/to/folder");
157+
when(() => mockShellService.runScript(GIT_TOP_LEVEL_PATH))
158+
.thenAnswer((_) async => throw ShellException("exception", null));
159+
160+
expect(
161+
// act
162+
() => repoService.setup(),
163+
// assert
164+
throwsA(isA<InvalidValidGitPathException>()),
165+
);
166+
});
126167
});
127168

128169
test(

0 commit comments

Comments
 (0)