Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent fetch latest changes when the url was cloned #7

Merged
merged 4 commits into from
Sep 18, 2023
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning].

- /

## [1.4.1] - 2023-09-18


### Fixed

- Now the urls are only refreshed when are already cloned and not always

## [1.4.0] - 2023-09-17

### Added
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ Before to run this app requires the following

You can download the binaries on the [releases](https://github.com/jsilverdev/config_props_extractor/releases) section.

Or git checkout to a desired tag (for example v1.4.0):
Or git checkout to a desired tag (for example v1.4.1):

```bash
git checkout tags/v1.4.0
git checkout tags/v1.4.1
```

And then run the binary for your platform
Expand Down
11 changes: 7 additions & 4 deletions lib/models/git_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ class GitRepo {
final Directory gitDir;
final String gitUrl;
String branch;
final bool fromRemote;
final bool toClone;
bool wasCloned = false;

GitRepo({
required this.gitDir,
required String gitPath,
required this.gitUrl,
required this.branch,
required this.fromRemote,
});
required this.toClone,
}) : gitDir = Directory(gitPath).absolute;

bool get existsOnLocal => gitDir.existsSync();
}
30 changes: 14 additions & 16 deletions lib/services/repo_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:process_run/process_run.dart';
Expand Down Expand Up @@ -40,10 +39,10 @@ class RepoService {
}

return GitRepo(
gitDir: Directory(gitPath),
gitPath: gitPath,
gitUrl: gitUrl,
branch: _appConfig.gitBranch,
fromRemote: isGitUrl,
toClone: isGitUrl,
);
}

Expand All @@ -56,10 +55,10 @@ class RepoService {
}

Future<void> tryCloning(final GitRepo gitRepo) async {
if (!gitRepo.fromRemote) return;
if (!gitRepo.toClone) return;

final exists = await _deleteGitDirIfHasNoCommits(gitRepo.gitDir);
if(exists) return;
await _deleteIfRepoHasNoCommits(gitRepo);
if (gitRepo.existsOnLocal) return;

final runScript = _shellService.runScript(constants.GIT_CLONE.format([
gitRepo.gitUrl,
Expand All @@ -69,27 +68,25 @@ class RepoService {

await _timeoutFor(runScript);
log.i("Successfully cloned at: ${gitRepo.gitDir.absolute.path}");
gitRepo.wasCloned = true;
}

// * Necessary for windows
Future<bool> _deleteGitDirIfHasNoCommits(Directory gitDir) async {
bool exists = gitDir.existsSync();
_shellService.moveShellTo(gitDir.absolute.path);
Future<void> _deleteIfRepoHasNoCommits(GitRepo gitRepo) async {
_shellService.moveShellTo(gitRepo.gitDir.path);
try {
if (exists) {
if (gitRepo.existsOnLocal) {
await _shellService.runScript(constants.GIT_REV_PARSE_HEAD);
}
} on AppShellException {
gitDir.deleteSync(recursive: true);
exists = false;
gitRepo.gitDir.deleteSync(recursive: true);
}
_shellService.popShell();
return exists;
}

Future<void> checkGitPath(final GitRepo gitRepo) async {
final String gitPath = gitRepo.gitDir.absolute.path;
if (!Directory(gitPath).existsSync()) {
final String gitPath = gitRepo.gitDir.path;
if (!gitRepo.existsOnLocal) {
throw FolderNotFoundException(path: gitPath);
}

Expand All @@ -106,8 +103,9 @@ class RepoService {
Future<void> tryFetchingChanges(final GitRepo gitRepo) async {
final bool forceRemote = _appConfig.gitForceRemote;
await _validateGitBranch(gitRepo);
final bool forceRefresh = gitRepo.toClone && !gitRepo.wasCloned;

if (!forceRemote && !gitRepo.fromRemote) return;
if (!forceRemote && !forceRefresh) return;

log.i('Getting latests changes for "${gitRepo.branch}" branch');

Expand Down
Empty file removed test/_data/results/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions test/_data/results/example_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Created at: 2022-02-02 0:00:00

8 changes: 3 additions & 5 deletions test/helpers/git_helper.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:config_props_extractor/models/git_repo.dart';

const reposFolder = "test/_data";
Expand All @@ -11,12 +9,12 @@ GitRepo generateGitRepo({
String gitPath = gitPath,
String gitUrl = gitUrl,
String branch = gitBranch,
bool fromRemote = false,
bool toClone = false,
}) {
return GitRepo(
gitDir: Directory(gitPath),
gitPath: gitPath,
gitUrl: gitUrl,
branch: branch,
fromRemote: fromRemote,
toClone: toClone,
);
}
31 changes: 17 additions & 14 deletions test/services/kube_config_service_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'dart:io';

import 'package:clock/clock.dart';
import 'package:config_props_extractor/constants/constants.dart';
import 'package:config_props_extractor/models/properties_string_config.dart';
import 'package:config_props_extractor/services/kube_config_service.dart';
import 'package:config_props_extractor/utils/kube_utils.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import '../helpers/mocks.dart';

Expand Down Expand Up @@ -71,47 +70,51 @@ void main() {
});

group("Save Data to File", () {
final File testFile = File(
path.absolute("test/_data/results", PROPERTIES_FILENAME),
final File fileShouldExists = File(
path.absolute("test/_data/results/example_file"),
);
final File fileShouldNotExists = File(
path.absolute("test/_data/results/new_example_file"),
);

setUp(() {
if (testFile.existsSync()) testFile.deleteSync();
if (fileShouldNotExists.existsSync()) fileShouldNotExists.deleteSync();
kubeConfigService = KubeConfigService(mockAppConfig, mockShellService);
});

tearDown(() {
testFile.deleteSync();
if (fileShouldNotExists.existsSync()) {
fileShouldNotExists.deleteSync();
}
});

void testSaveData() {
void testSaveData(File file) {
//act
withClock(
Clock.fixed(DateTime(2022, 02, 02, 0, 0, 0)),
() => kubeConfigService.saveDataAsPropertiesFile(
fileName: testFile.path,
fileName: file.path,
config: PropertiesStringConfig.properties(),
),
);

// assert
expect(testFile.existsSync(), true);
expect(
testFile.readAsStringSync(), "# Created at: 2022-02-02 0:00:00\n\n");
expect(fileShouldExists.existsSync(), true);
expect(fileShouldExists.readAsStringSync(),
"# Created at: 2022-02-02 0:00:00\n\n");
}

test(
'Should save Data as Properties',
() async {
testSaveData();
testSaveData(fileShouldNotExists);
},
);

test(
'Should delete file with same name and then save Data as Properties',
() async {
testFile.createSync();
testSaveData();
testSaveData(fileShouldExists);
},
);
});
Expand Down
18 changes: 10 additions & 8 deletions test/services/repo_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void main() {
expect(path.equals(gitRepo.gitDir.path, gitPath), equals(true));
expect(gitRepo.gitUrl, "");
expect(gitRepo.branch, gitBranch);
expect(gitRepo.fromRemote, false);
expect(gitRepo.toClone, false);
},
);

Expand All @@ -63,7 +63,7 @@ void main() {
expect(path.equals(gitRepo.gitDir.path, gitPath), equals(true));
expect(gitRepo.gitUrl, gitUrl);
expect(gitRepo.branch, gitBranch);
expect(gitRepo.fromRemote, true);
expect(gitRepo.toClone, true);
},
);
});
Expand All @@ -75,7 +75,7 @@ void main() {
setUp(() {
gitRepo = generateGitRepo(
gitPath: gitDirToClone.path,
fromRemote: true,
toClone: true,
);
});

Expand All @@ -90,10 +90,10 @@ void main() {
() async {
// arrange
final localGitRepo = GitRepo(
gitDir: Directory(gitPath),
gitPath: gitPath,
gitUrl: "",
branch: gitBranch,
fromRemote: false,
toClone: false,
);
// act
await repoService.tryCloning(localGitRepo);
Expand Down Expand Up @@ -126,6 +126,7 @@ void main() {
// act
await repoService.tryCloning(gitRepo);
// assert
expect(gitRepo.wasCloned, equals(true));
verify(() => mockShellService.runScript(GIT_REV_PARSE_HEAD));
verify(
() => mockShellService.runScript(GIT_CLONE.format([
Expand Down Expand Up @@ -156,6 +157,7 @@ void main() {
await repoService.tryCloning(gitRepo);

// assert
expect(gitRepo.existsOnLocal, equals(true));
verify(
() => mockShellService.runScript(
GIT_REV_PARSE_HEAD,
Expand Down Expand Up @@ -260,7 +262,7 @@ void main() {
'Should fetch successfully if force remote is enabled',
() async {
// arrange
final gitRepo = generateGitRepo(fromRemote: false);
final gitRepo = generateGitRepo(toClone: false);
when(() => mockAppConfig.gitSSLEnabled).thenReturn(true);
when(() => mockAppConfig.gitForceRemote).thenReturn(true);
when(() => mockAppConfig.maxDuration).thenReturn(Duration.zero);
Expand All @@ -278,10 +280,10 @@ void main() {
);

test(
'Should fetch successfully if it is from remote',
"Should fetch successfully if it is set to clone but wasn't cloned",
() async {
// arrange
final gitRepo = generateGitRepo(fromRemote: true);
final gitRepo = generateGitRepo(toClone: true);
when(() => mockAppConfig.gitSSLEnabled).thenReturn(true);
when(() => mockAppConfig.gitForceRemote).thenReturn(false);
when(() => mockAppConfig.maxDuration).thenReturn(Duration.zero);
Expand Down