Skip to content

Commit

Permalink
Merge pull request #29 from iwatakeshi/fix/clone
Browse files Browse the repository at this point in the history
Fix `clone` function
  • Loading branch information
iwatakeshi authored Feb 18, 2024
2 parents 1ee4e65 + 694c4c1 commit a796a50
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 157 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
Expand All @@ -38,7 +38,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -49,7 +49,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -63,4 +63,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
1 change: 0 additions & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

The MIT License (MIT)

Copyright (c) 2019 Takeshi Iwana
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ console.log(await gitly('iwatakeshi/gitly', '/path/to/extracted/folder/'))

## Options

```typescript
````typescript
interface GitlyOptions {
/**
* Use cache only (default: undefined)
Expand Down Expand Up @@ -101,7 +101,7 @@ interface GitlyOptions {
*/
backend?: 'axios' | 'git'
}
```
````

## Interfaces

Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prefer-arrow": "^1.2.3",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"shelljs": "^0.8.5",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
Expand All @@ -39,7 +40,8 @@
"build": "tsc",
"test": "jest",
"lint": "eslint -c .eslintrc.js --ext .ts src",
"lint:fix": "npm run lint --fix"
"lint:fix": "npm run lint --fix",
"format": "prettier --write \"src/**/*.ts\" \"*.json\" \"*.md\""
},
"types": "lib/main.d.ts",
"keywords": [
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ export default interface GitlyOptions {
* ```
*/
backend?: 'axios' | 'git'
/**
* Set git options (default: undefined)
*/
git?: {
depth?: number
}
}
2 changes: 1 addition & 1 deletion src/interfaces/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default interface URLInfo {
hostname: string
hash: string
href: string
path: string,
path: string
repository: string
owner: string
type: string
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { default } from './utils/gitly'
export { default as download } from './utils/download'
export { default as extract } from './utils/extract'
export { default as parse } from './utils/parse'
export { default as clone } from './utils/clone'
export { default as clone } from './utils/clone'
19 changes: 12 additions & 7 deletions src/utils/__test__/archive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import parse from '../parse'
import { getUrl } from '../archive'
import { getArchiveUrl } from '../archive'
import { getArchivePath } from '../archive'
const isWin32 = process.platform === 'win32'

describe('utils/archive', () => {
describe('getUrl()', () => {
it('should return a github url to the zipped file', () => {
expect(getUrl(parse('iwatakeshi/test'))).toEqual(
expect(getArchiveUrl(parse('iwatakeshi/test'))).toEqual(
'https://github.com/iwatakeshi/test/archive/master.tar.gz'
)
})

it('should return a bitbucket url to the zipped file', () => {
expect(getUrl(parse('bitbucket:iwatakeshi/test'))).toEqual(
expect(getArchiveUrl(parse('bitbucket:iwatakeshi/test'))).toEqual(
'https://bitbucket.org/iwatakeshi/test/get/master.tar.gz'
)
})

it('should return a gitlab url to the zipped file', () => {
expect(getUrl(parse('gitlab:iwatakeshi/test'))).toEqual(
expect(getArchiveUrl(parse('gitlab:iwatakeshi/test'))).toEqual(
'https://gitlab.com/iwatakeshi/test/-/archive/master/test-master.tar.gz'
)
})

it('should return a custom url to the zipped file', () => {
expect(
getUrl(parse('iwatakeshi/test'), {
getArchiveUrl(parse('iwatakeshi/test'), {
url: {
filter(info) {
return `https://domain.com${info.path}/repo/archive.tar.gz?ref=${info.type}`
Expand All @@ -41,8 +41,13 @@ describe('utils/archive', () => {
describe('getArchivePath()', () => {
it('should return a path to the zipped file', () => {
expect(getArchivePath(parse('iwatakeshi/test'))).toEqual(
isWin32 ? expect.stringMatching(/\.gitly\\github\\iwatakeshi\\test\\master\.tar\.gz/) :
expect.stringMatching(/\.gitly\/github\/iwatakeshi\/test\/master\.tar\.gz/)
isWin32
? expect.stringMatching(
/\.gitly\\github\\iwatakeshi\\test\\master\.tar\.gz/
)
: expect.stringMatching(
/\.gitly\/github\/iwatakeshi\/test\/master\.tar\.gz/
)
)
})
})
Expand Down
23 changes: 9 additions & 14 deletions src/utils/__test__/clone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import exists from '../exists';
import exists from '../exists'
import clone from '../clone'
import { rm } from 'shelljs'
import { join } from 'path';

import { join } from 'path'

describe('utils/clone', () => {
const destination = join(__dirname, 'output', 'clone', 'example')
const options = {
temp: join(__dirname, 'output', 'clone', '.gitcopy'),
temp: join(__dirname, 'output', 'clone'),
backend: 'git' as 'git' | 'axios',
}

beforeEach(async () => {
rm('-rf', join(__dirname, 'output', 'clone', '.gitcopy'))
})
afterEach(async () => {
rm('-rf', destination)
beforeAll(() => {
rm('-rf', join(__dirname, 'output', 'clone'))
})

afterAll(async () => {
rm('-rf', join(__dirname, 'output', 'clone', '.gitcopy'))
afterAll(() => {
rm('-rf', join(__dirname, 'output', 'clone'))
})
it('should clone the repository', async () => {
const result = await clone('lukeed/gittar', options)
expect(await exists(result)).toBe(true)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@ import { rm } from 'shelljs'
import download from '../download'
import { GitlyDownloadError } from '../error'

describe('utils/fetch (no cache)', () => {
describe('utils/download (no cache)', () => {
const options = {
temp: join(__dirname, 'output', 'fetch', '.gitcopy'),
temp: join(__dirname, 'output', 'download'),
}
beforeEach(async () => {
rm('-rf', join(__dirname, 'output', 'fetch', '.gitcopy'))

beforeAll(() => {
rm('-rf', join(__dirname, 'output', 'download'))
})

afterAll(async () => {
rm('-rf', join(__dirname, 'output', 'fetch', '.gitcopy'))
beforeEach(() => {
rm('-rf', join(__dirname, 'output', 'download'))
})

afterAll(() => {
rm('-rf', join(__dirname, 'output', 'download'))
})

it('should fetch "lukeed/gittar"', async () => {
it('should download "lukeed/gittar"', async () => {
expect.assertions(2)
const path = await download('lukeed/gittar', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "lukeed/gittar#v0.1.1"', async () => {
it('should download "lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2)
const path = await download('lukeed/gittar#v0.1.1', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "https://github.com/lukeed/gittar"', async () => {
it('should download "https://github.com/lukeed/gittar"', async () => {
expect.assertions(2)
const path = await download('https://github.com/lukeed/gittar', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})
it('should fetch "https://github.com/lukeed/gittar#v0.1.1"', async () => {
it('should download "https://github.com/lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2)
const path = await download(
'https://github.com/lukeed/gittar#v0.1.1',
Expand All @@ -47,35 +52,35 @@ describe('utils/fetch (no cache)', () => {
expect(existsSync(path)).toBe(true)
})

it('should fetch "github.com/lukeed/gittar"', async () => {
it('should download "github.com/lukeed/gittar"', async () => {
expect.assertions(2)
const path = await download('github.com/lukeed/gittar', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "github.com/lukeed/gittar#v0.1.1"', async () => {
it('should download "github.com/lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2)
const path = await download('github.com/lukeed/gittar#v0.1.1', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "github:lukeed/gittar"', async () => {
it('should download "github:lukeed/gittar"', async () => {
expect.assertions(2)
const path = await download('github:lukeed/gittar', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "github:lukeed/gittar#v0.1.1"', async () => {
it('should download "github:lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2)
const path = await download('github:lukeed/gittar#v0.1.1', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "gitlab:Rich-Harris/buble#v0.15.2"', async () => {
it('should download "gitlab:Rich-Harris/buble#v0.15.2"', async () => {
expect.assertions(2)
const path = await download('gitlab:Rich-Harris/buble#v0.15.2', options)
expect(path).toBeTruthy()
Expand All @@ -101,22 +106,22 @@ describe('utils/fetch (no cache)', () => {
})
})

describe('utils/fetch (cached)', () => {
describe('utils/download (cached)', () => {
const options = {
temp: join(__dirname, 'output', 'fetch', 'cache'),
temp: join(__dirname, 'output', 'download', 'cache'),
cache: true,
}
const isCached = (ms: number) => Date.now() - ms <= 15

beforeAll(async () => {
rm('-rf', join(__dirname, 'output', 'fetch', 'cache'))
// Prefetch
rm('-rf', join(__dirname, 'output', 'download', 'cache'))
// Predownload
const path = await download('lukeed/gittar', { temp: options.temp })
expect(existsSync(path)).toBe(true)
})

afterAll(async () => {
rm('-rf', join(__dirname, 'output', 'fetch', 'cache'))
rm('-rf', join(__dirname, 'output', 'download', 'cache'))
})

it('should return a path to the cached zipped file', async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/__test__/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { GitlyDownloadError, GitlyErrorType, GitlyExtractError, GitlyFetchError, GitlyUknownError } from '../error'
import {
GitlyDownloadError,
GitlyErrorType,
GitlyExtractError,
GitlyFetchError,
GitlyUknownError,
} from '../error'

describe('utils/error', () => {
describe('GitlyFetchError', () => {
Expand Down
Loading

0 comments on commit a796a50

Please sign in to comment.