Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi Iwana committed Dec 19, 2020
1 parent 4f6fc0b commit f0fa539
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
23 changes: 13 additions & 10 deletions src/utils/__test__/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { join } from 'path'
import { rm } from 'shelljs'

import extract from '../extract'
import fetch from '../fetch'
import download from '../download'

describe('utils/extract', () => {
const destination = join(__dirname, 'output', 'extract', 'example')
const options = {
temp: join(__dirname, 'output', 'extract', '.gitcopy')
temp: join(__dirname, 'output', 'extract', '.gitcopy'),
}
beforeEach(async () => {
rm('-rf', join(__dirname, 'output', 'extract', '.gitcopy'))
Expand All @@ -23,14 +23,14 @@ describe('utils/extract', () => {

it('should extract "lukeed/gittar"', async () => {
// tslint:disable-next-line: no-floating-promises
const path = await fetch('lukeed/gittar', options)
const path = await download('lukeed/gittar', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should extract "lukeed/gittar#v0.1.1"', async () => {
// tslint:disable-next-line: no-floating-promises
const source = await fetch('lukeed/gittar#v0.1.1', options)
const source = await download('lukeed/gittar#v0.1.1', options)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand All @@ -39,7 +39,7 @@ describe('utils/extract', () => {
})

it('should extract "https://github.com/lukeed/gittar"', async () => {
const source = await fetch('https://github.com/lukeed/gittar', options)
const source = await download('https://github.com/lukeed/gittar', options)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand All @@ -48,7 +48,10 @@ describe('utils/extract', () => {
})

it('should extract "https://github.com/lukeed/gittar#v0.1.1"', async () => {
const source = await fetch('https://github.com/lukeed/gittar#v0.1.1', options)
const source = await download(
'https://github.com/lukeed/gittar#v0.1.1',
options
)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand All @@ -57,7 +60,7 @@ describe('utils/extract', () => {
})

it('should extract "github.com/lukeed/gittar"', async () => {
const source = await fetch('github.com/lukeed/gittar', options)
const source = await download('github.com/lukeed/gittar', options)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand All @@ -66,7 +69,7 @@ describe('utils/extract', () => {
})

it('should extract "github.com/lukeed/gittar#v0.1.1"', async () => {
const source = await fetch('github.com/lukeed/gittar#v0.1.1', options)
const source = await download('github.com/lukeed/gittar#v0.1.1', options)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand All @@ -75,7 +78,7 @@ describe('utils/extract', () => {
})

it('should extract "github:lukeed/gittar"', async () => {
const source = await fetch('github:lukeed/gittar', options)
const source = await download('github:lukeed/gittar', options)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand All @@ -84,7 +87,7 @@ describe('utils/extract', () => {
})

it('should extract "github:lukeed/gittar#v0.1.1"', async () => {
const source = await fetch('github:lukeed/gittar#v0.1.1', options)
const source = await download('github:lukeed/gittar#v0.1.1', options)
const path = await extract(source, destination, options)
expect(existsSync(source)).toBe(true)
expect(source).toBeTruthy()
Expand Down
40 changes: 22 additions & 18 deletions src/utils/__test__/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { join } from 'path'
import { rm } from 'shelljs'

import { GitlyDownloadError } from '../error'
import fetch from '../fetch'
import download from '../download'

describe('utils/fetch (no cache)', () => {
const options = {
temp: join(__dirname, 'output', 'fetch', '.gitcopy')
temp: join(__dirname, 'output', 'fetch', '.gitcopy'),
}
beforeEach(async () => {
rm('-rf', join(__dirname, 'output', 'fetch', '.gitcopy'))
Expand All @@ -19,76 +19,81 @@ describe('utils/fetch (no cache)', () => {

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

it('should fetch "lukeed/gittar#v0.1.1"', async () => {
expect.assertions(2)
const path = await fetch('lukeed/gittar#v0.1.1', options)
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 () => {
expect.assertions(2)
const path = await fetch('https://github.com/lukeed/gittar', options)
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 () => {
expect.assertions(2)
const path = await fetch('https://github.com/lukeed/gittar#v0.1.1', options)
const path = await download(
'https://github.com/lukeed/gittar#v0.1.1',
options
)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should fetch "github.com/lukeed/gittar"', async () => {
expect.assertions(2)
const path = await fetch('github.com/lukeed/gittar', options)
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 () => {
expect.assertions(2)
const path = await fetch('github.com/lukeed/gittar#v0.1.1', options)
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 () => {
expect.assertions(2)
const path = await fetch('github:lukeed/gittar', options)
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 () => {
expect.assertions(2)
const path = await fetch('github:lukeed/gittar#v0.1.1', options)
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 () => {
expect.assertions(2)
const path = await fetch('gitlab:Rich-Harris/buble#v0.15.2', options)
const path = await download('gitlab:Rich-Harris/buble#v0.15.2', options)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)
})

it('should return an empty string when a repo is not found', async () => {
expect.assertions(1)
expect(await fetch('github:doesnotexist123xyz/gittar#v0.1.1')).toEqual('')
expect(await download('github:doesnotexist123xyz/gittar#v0.1.1')).toEqual(
''
)
})

it('should throw an error when a repo is not found (with options', async () => {
expect.assertions(1)
try {
await fetch('github:doesnotexist123xyz/gittar#v0.1.1', {
throw: true
await download('github:doesnotexist123xyz/gittar#v0.1.1', {
throw: true,
})
} catch (error) {
expect(error).toBeInstanceOf(GitlyDownloadError)
Expand All @@ -99,14 +104,14 @@ describe('utils/fetch (no cache)', () => {
describe('utils/fetch (cached)', () => {
const options = {
temp: join(__dirname, 'output', 'fetch', 'cache'),
cache: true
cache: true,
}
const isCached = (ms: number) => Date.now() - ms <= 15

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

Expand All @@ -116,10 +121,9 @@ describe('utils/fetch (cached)', () => {

it('should return a path to the cached zipped file', async () => {
const start = Date.now()
const path = await fetch('lukeed/gittar', options)
const path = await download('lukeed/gittar', options)
expect(isCached(start)).toBe(true)
expect(path).toBeTruthy()
expect(existsSync(path)).toBe(true)

})
})

0 comments on commit f0fa539

Please sign in to comment.