Skip to content

Commit

Permalink
[ISSUE-415] By default cleanup clone target folder if already existing (
Browse files Browse the repository at this point in the history
#435)

* [ISSUE-415] By default cleanup clone target folder if already existing

* [ISSUE-415] Improved tests checking rmSync and mkdirSync invocation

* [ISSUE-415] Refactored clone test

* Removed useless mkdir as already created by git clone
  • Loading branch information
lampajr authored May 29, 2023
1 parent 7355061 commit b1bf2ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
11 changes: 6 additions & 5 deletions src/service/git/git-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ export class GitCLIService {
* @param branch branch which should be cloned
*/
async clone(from: string, to: string, branch: string) {
if (!fs.existsSync(to)) {
// don't use this.git since it will configure git with local user.name and user.email which requires cwd to be a git repo
await simpleGit().clone(from, to, ["--quiet", "--shallow-submodules", "--no-tags", "--branch", branch]);
} else {
this.logger.warn(`Folder ${to} already exist. Won't clone`);
if (fs.existsSync(to)) {
this.logger.warn(`Folder ${to} already exist. Deleting it`);
fs.rmSync(to, { recursive: true, force: true });
}

// don't use this.git since it will configure git with local user.name and user.email which requires cwd to be a git repo
await simpleGit().clone(from, to, ["--quiet", "--shallow-submodules", "--no-tags", "--branch", branch]);
}

/**
Expand Down
28 changes: 16 additions & 12 deletions test/unitary/service/git/git-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,29 @@ test("version", async () => {
});

test.each([
["succes: destination does not exist", false],
["failure: destination exists", true],
])("clone %p", async (_title: string, destExist: boolean) => {
["destination does not exist", false, 0],
["destination exists", true, 1],
])("clone %p", async (_title: string, destExist: boolean, numRmCalls: number) => {
// Setup
const dest = path.join(__dirname, "git-clone-test");

// test for failure
if (destExist) {
// create the folder before cloning the repo
fs.mkdirSync(dest);
await git.clone(cwd, dest, "main");
const files = fs.readdirSync(dest);
expect(files.length).toBe(0);
}
// test for success
else {
await git.clone(cwd, dest, "main");
expect(fs.existsSync(dest)).toBe(true);
}

const rmSyncSpy = jest.spyOn(fs, "rmSync");

await git.clone(cwd, dest, "main");

expect(rmSyncSpy).toBeCalledTimes(numRmCalls);

expect(fs.existsSync(dest)).toBe(true);
const files = fs.readdirSync(dest);
expect(files.length).toBeGreaterThan(0);

// Clean up
rmSyncSpy.mockRestore();
fs.rmSync(dest, { recursive: true });
});

Expand Down

0 comments on commit b1bf2ec

Please sign in to comment.