Skip to content

Commit

Permalink
replace unzipper with extract-zip
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 10, 2022
1 parent 1adbe37 commit ae4d415
Show file tree
Hide file tree
Showing 13 changed files with 622 additions and 480 deletions.
15 changes: 0 additions & 15 deletions commons/constants.spec.ts

This file was deleted.

23 changes: 23 additions & 0 deletions commons/loggerTexts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Tests constants file
*
* @group unit/file/loggerTexts
*/

import type { TextFunction } from '../interfaces/interfaces'
import { EXTRACT_ZIP, READ_CONFIG } from './loggerTexts'

describe('constants', () => {

it('should add the reason in the READ_CONFIG description', () => {
expect((READ_CONFIG.fail as TextFunction)('the reason')).toEqual('Error loading localstore.json from filesystem: the reason')
})

it('should add the downloadPath in the EXTRACT_ZIP description', () => {
expect((EXTRACT_ZIP.success as TextFunction)('the download-path')).toEqual('Successfully extracted to "the download-path"')
})

it('should add the error in the EXTRACT_ZIP description', () => {
expect((EXTRACT_ZIP.fail as TextFunction)('the-error')).toEqual('Failed to extract binary the-error')
})
})
26 changes: 16 additions & 10 deletions commons/loggerTexts.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import { LogConfig, LoggerConfig, TextFunction } from '../interfaces/interfaces'
import { LoggerConfig, StringLoggerConfig, TextFunction } from '../interfaces/interfaces'

export const LOAD_CONFIG: LoggerConfig = {
export const LOAD_CONFIG: StringLoggerConfig = {
start: 'Downloading local storage file',
success: 'localstore.json file downloaded successfully!',
fail: 'Error downloading localstore.json',
}

export const DOWNLOAD_ZIP: LogConfig<TextFunction, string> = {
export const DOWNLOAD_ZIP: LoggerConfig<TextFunction, string> = {
start: 'Downloading binary...',
success: downloadPath => `Successfully downloaded to "${downloadPath}.zip"`,
fail: 'Failed to download binary',
}

export const EXTRACT_ZIP: LogConfig<TextFunction, string> = {
start: 'Downloading binary...',
success: downloadPath => `Successfully downloaded and extracted to "${downloadPath}"`,
fail: 'Failed to download binary',
export const EXTRACT_ZIP: LoggerConfig<TextFunction, TextFunction> = {
start: 'Extracting binary...',
success: downloadPath => `Successfully extracted to "${downloadPath}"`,
fail: error => `Failed to extract binary ${error}`,
}

export const DELETE_ZIP: StringLoggerConfig = {
start: 'Deleting zip...',
success: 'Successfully deleted zip file',
fail: 'Failed to delete zip file',
}

export const READ_CONFIG: LogConfig<string, TextFunction> = {
export const READ_CONFIG: LoggerConfig<string, TextFunction> = {
start: 'Reading local storage file from filesystem',
success: 'Successfully loaded localstore.json from filesystem',
fail: reason => `Error loading localstore.json from filesystem: ${reason}`,
}

export const RESOLVE_VERSION: LoggerConfig = {
export const RESOLVE_VERSION: StringLoggerConfig = {
start: 'Resolving version to branch position...',
success: 'Version resolved!',
fail: 'Error resolving version!',
}

export const SEARCH_BINARY: LoggerConfig = {
export const SEARCH_BINARY: StringLoggerConfig = {
start: 'Searching for binary...',
success: 'Binary found.',
fail: 'No binary found!',
Expand Down
62 changes: 58 additions & 4 deletions download.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { existsSync, readFile as fsReadFile, writeFile as fsWriteFile, unlink as
import * as mockFs from 'mock-fs'
/* eslint-disable-next-line import/no-namespace */
import * as fetch from 'node-fetch'
import { join, resolve } from 'path'
import { join as pathJoin, resolve } from 'path'
import { MaybeMocked } from 'ts-jest/dist/utils/testing'
import { mocked } from 'ts-jest/utils'
import { promisify } from 'util'
Expand All @@ -29,9 +29,13 @@ jest.mock('prompts')

describe('[int] download chromium', () => {

const chromeZip10 = join(__dirname, 'chrome-linux-x64-10.0.0.0.zip')
const chromeZip20 = join(__dirname, 'chrome-linux-x64-20.0.0.0.zip')
const localStoreFile = join(__dirname, 'localstore.json')
// https://stackoverflow.com/questions/2438800/what-is-the-smallest-legal-zip-jar-file
const minimalValidZipfile = new Uint8Array([80, 75, 5, 6].concat(Array.from({length: 18}).map(() => 0)))

const chromeZip10 = pathJoin(__dirname, 'chrome-linux-x64-10.0.0.0.zip')
const chromeZip20 = pathJoin(__dirname, 'chrome-linux-x64-20.0.0.0.zip')
const chromeFolder20 = pathJoin(__dirname, 'chrome-linux-x64-20.0.0.0')
const localStoreFile = pathJoin(__dirname, 'localstore.json')

let promptsMock: MaybeMocked<typeof prompts>
let nodeFetchMock: MaybeMocked<typeof fetch>
Expand Down Expand Up @@ -164,6 +168,56 @@ describe('[int] download chromium', () => {
})
})

it('should select, download and extract a version', async () => {
mockNodeFetch(nodeFetchMock, {
params: {
tags: ['10.0.0.0', '20.0.0.0']
},
config: {
chromeZip: {
contentLength: minimalValidZipfile.length,
}
}
})
promptsMock.mockReturnValue({ version: '20.0.0.0' })

const rustedPromise = rusted(['/some/path/to/node', '/some/path/to/rusted-chromium', '--unzip'], 'linux')

chromeZipStream.push(minimalValidZipfile)
chromeZipStream.end()

await rustedPromise

expect(existsSync(chromeFolder20)).toBe(true)
expect(existsSync(chromeZip10)).toBe(false)
expect(existsSync(chromeZip20)).toBe(false)

expect(promptsMock).toHaveBeenCalledTimes(1)
expect(promptsMock).toHaveBeenCalledWith({
choices: [
new MappedVersion({
major: 20,
minor: 0,
branch: 0,
patch: 0,
disabled: false,
}),
new MappedVersion({
major: 10,
minor: 0,
branch: 0,
patch: 0,
disabled: false,
}),
],
hint: 'for linux x64',
message: 'Select a version',
name: 'version',
type: 'select',
warn: 'This version seems to not have a binary',
})
})

it('should mark a chrome version as disabled, on no binary found', async () => {
mockNodeFetch(nodeFetchMock, {
params: {
Expand Down
Loading

0 comments on commit ae4d415

Please sign in to comment.