Passed
Push — main ( 3f259d...71e4ad )
by Patrick
02:34
created

tests/lib/Repository.test.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 93
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 81
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 2
mnd 2
bc 2
fnc 0
bpm 0
cpm 0
noi 0
1
import { Github } from '@/index';
2
import { Repository } from '@/lib/Repository';
3
import { execSync } from 'child_process';
4
import fs, { existsSync } from 'fs';
5
import path from 'path';
6
import child_process from 'child_process';
7
8
const tempPath = `${__dirname}/../fixtures/temp`;
9
10
beforeEach(() => {
11
    if (existsSync(`${tempPath}/laravel/framework`)) {
12
        execSync(`rm -rf ${tempPath}/laravel/framework`);
13
    }
14
});
15
16
it('sets properties correctly on create', async () => {
17
    const repository = new Repository('owner/name', 'path');
18
19
    expect(repository).toBeInstanceOf(Repository);
20
21
    expect(repository.name).toBe('name');
22
    expect(repository.owner).toBe('owner');
23
    expect(repository.path).toBe('path/owner/name');
24
25
    expect(repository).toMatchSnapshot();
26
});
27
28
it('gets the current branch names', async () => {
29
    const repository = new Repository('permafrost-dev/codeboost', tempPath);
30
    repository.path = `${__dirname}/../..`;
31
32
    const mainBranchName = 'main';
33
    const branch = await repository.currentBranch();
34
35
    expect(branch).toBe(mainBranchName);
36
});
37
38
it('gets a list of local branches', async () => {
39
    const repository = new Repository('permafrost-dev/codeboost', tempPath);
40
    repository.path = `${__dirname}/../..`;
41
42
    const mainBranchName = 'main';
43
    const branches = await repository.localBranches();
44
45
    expect(branches.all).toContain(mainBranchName);
46
});
47
48
it('checks if it is on a branch', async () => {
49
    const repository = new Repository('permafrost-dev/codeboost', tempPath);
50
    repository.path = `${__dirname}/../..`;
51
52
    const mainBranchName = 'main';
53
    const isOnBranch = await repository.onBranch(mainBranchName);
54
55
    expect(isOnBranch).toBeTruthy();
56
});
57
58
if (typeof process.env.CI !== 'undefined') {
59
    it('checks out a new branch', async () => {
60
        const repository = new Repository('permafrost-dev/codeboost', tempPath);
61
        repository.path = `${__dirname}/../..`;
62
63
        const randomInt = Math.floor(Math.random() * 10000) + 1;
64
        const newBranchName = `test-branch-${randomInt}`;
65
        await repository.checkout(newBranchName);
66
67
        const isOnBranch = await repository.onBranch(newBranchName);
68
69
        expect(isOnBranch).toBeTruthy();
70
    });
71
}
72
73
it('throws an error when forking a repository onto its own user', async () => {
74
    const repository = new Repository('permafrost-dev/does-not-exist', tempPath);
75
    Github.setCache({ currentUser: { login: 'permafrost-dev' } });
76
77
    await expect(repository.createFork()).rejects.toThrow();
78
});
79
80
it('throws an error when forking a repository that does not exist', async () => {
81
    const repository = new Repository('permafrost-dev/does-not-exist', tempPath);
82
    Github.setCache({ currentUser: { login: 'patinthehat' } });
83
84
    await expect(repository.createFork()).rejects.toThrow();
85
});
86
87
it('throws an error when pushing to a fork on a repository that does not exist', async () => {
88
    const repository = new Repository('permafrost-dev/does-not-exist', tempPath);
89
    Github.setCache({ currentUser: { login: 'patinthehat' } });
90
91
    await expect(repository.pushToFork('missing-branch')).rejects.toThrow();
92
});
93
94
// it('does nothing if cloning a repository that already exists locally', async () => {
95
//     const repository = new Repository('laravel/framework', tempPath);
96
//     repository.path = __dirname;
97
98
//     await expect(repository.clone()).toBeTruthy();
99
// });
100
101
// it('creates the parent directory if it does not exist', async () => {
102
//     // Mock the `dirname` and `existsSync` functions.
103
//     jest.spyOn(path, 'dirname').mockReturnValue(tempPath);
104
//     jest.spyOn(fs, 'existsSync').mockReturnValue(false)
105
//         .mockReturnValueOnce(true);
106
//     const mkdirMock = jest.mock('fs', () => {
107
//         return {mkdirSync: jest.fn().mockReturnValue(''),};
108
//     });
109
110
//     const execSpy = jest.mock('child_process', () => {
111
//         return {execSync: jest.fn().mockReturnValue(''),};
112
//     });
113
114
//     const repo = new Repository('owner/name', tempPath + '/owner/name');
115
116
//     child_process.execSync = execSpy.fn();
117
//     fs.mkdirSync = mkdirMock.fn();
118
119
//     await expect(repo.clone()).toBeTruthy();
120
121
//     jest.restoreAllMocks();
122
// });
123
124
// it('clones the repository if it does not exist', async () => {
125
//     // Mock the `dirname` and `existsSync` functions.
126
//     const dirnameSpy = jest.spyOn(path, 'dirname').mockReturnValue('/parent/dir');
127
//     const existsSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
128
//     const execSpy = jest.mock('child_process', () => {
129
//         return {execSync: jest.fn().mockReturnValue(''),};
130
//     });
131
//     const mocks = [ dirnameSpy, existsSpy ];
132
133
//     // Create an instance of the class.
134
//     const repo = new Repository('owner/name', tempPath + '/owner/name');
135
136
//     await expect(repo.clone()).toBeTruthy();
137
138
//     mocks.map(mock => mock.mockRestore());
139
//     jest.restoreAllMocks();
140
// });
141