diff --git a/CHANGELOG.md b/CHANGELOG.md index ba39abb..d7c7941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 0b7345b..61f3839 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/models/git_repo.dart b/lib/models/git_repo.dart index 0cc3f5e..17f4aae 100644 --- a/lib/models/git_repo.dart +++ b/lib/models/git_repo.dart @@ -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(); } diff --git a/lib/services/repo_service.dart b/lib/services/repo_service.dart index ea5ce06..709ed81 100644 --- a/lib/services/repo_service.dart +++ b/lib/services/repo_service.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'package:path/path.dart' as path; import 'package:process_run/process_run.dart'; @@ -40,10 +39,10 @@ class RepoService { } return GitRepo( - gitDir: Directory(gitPath), + gitPath: gitPath, gitUrl: gitUrl, branch: _appConfig.gitBranch, - fromRemote: isGitUrl, + toClone: isGitUrl, ); } @@ -56,10 +55,10 @@ class RepoService { } Future 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, @@ -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 _deleteGitDirIfHasNoCommits(Directory gitDir) async { - bool exists = gitDir.existsSync(); - _shellService.moveShellTo(gitDir.absolute.path); + Future _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 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); } @@ -106,8 +103,9 @@ class RepoService { Future 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'); diff --git a/test/_data/results/.gitkeep b/test/_data/results/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/test/_data/results/example_file b/test/_data/results/example_file new file mode 100644 index 0000000..db12753 --- /dev/null +++ b/test/_data/results/example_file @@ -0,0 +1,2 @@ +# Created at: 2022-02-02 0:00:00 + diff --git a/test/helpers/git_helper.dart b/test/helpers/git_helper.dart index 6ffef60..6b23e9f 100644 --- a/test/helpers/git_helper.dart +++ b/test/helpers/git_helper.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:config_props_extractor/models/git_repo.dart'; const reposFolder = "test/_data"; @@ -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, ); } diff --git a/test/services/kube_config_service_test.dart b/test/services/kube_config_service_test.dart index 4b30822..f7d3370 100644 --- a/test/services/kube_config_service_test.dart +++ b/test/services/kube_config_service_test.dart @@ -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'; @@ -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); }, ); }); diff --git a/test/services/repo_service_test.dart b/test/services/repo_service_test.dart index ffc865c..7012f6c 100644 --- a/test/services/repo_service_test.dart +++ b/test/services/repo_service_test.dart @@ -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); }, ); @@ -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); }, ); }); @@ -75,7 +75,7 @@ void main() { setUp(() { gitRepo = generateGitRepo( gitPath: gitDirToClone.path, - fromRemote: true, + toClone: true, ); }); @@ -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); @@ -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([ @@ -156,6 +157,7 @@ void main() { await repoService.tryCloning(gitRepo); // assert + expect(gitRepo.existsOnLocal, equals(true)); verify( () => mockShellService.runScript( GIT_REV_PARSE_HEAD, @@ -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); @@ -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);