Skip to content

Commit

Permalink
use toMinimalFormattedString for the localstore.json
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 24, 2022
1 parent 219a7da commit f32a994
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 16 deletions.
5 changes: 3 additions & 2 deletions import.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mocked } from 'ts-jest/utils'
import { promisify } from 'util'

import { rusted } from './rusted'
import { Store } from './store/Store'
import { mockNodeFetch, IMockAdditional, getJestTmpFolder } from './test/int.utils'
import { createStore } from './test/test.utils'

Expand Down Expand Up @@ -81,7 +82,7 @@ describe('[int] import store', () => {

const storedStore = await readFile(localStoreFile, { encoding: 'utf-8' })

expect(storedStore).toEqual(JSON.stringify(expectedStore, null, 4))
expect(storedStore).toEqual(new Store(expectedStore).toMinimalFormattedString())
})

it('should import by URL', async () => {
Expand All @@ -106,6 +107,6 @@ describe('[int] import store', () => {

const storedStore = await readFile(localStoreFile, { encoding: 'utf-8' })

expect(storedStore).toEqual(JSON.stringify(expectedStore, null, 4))
expect(storedStore).toEqual(new Store(expectedStore).toMinimalFormattedString())
})
})
64 changes: 64 additions & 0 deletions store/Store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,69 @@ describe('Store', () => {
}
}`)
})

it('should format the store with indentation without indenting the arrays', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

expect(store.toMinimalFormattedString()).toEqual(`{
"win": {
"x64": [],
"x86": []
},
"linux": {
"x64": ["10.0.0.0","20.0.0.0","30.0.0.0"],
"x86": ["10.0.0.0"]
},
"mac": {
"x64": [],
"arm": []
}
}`)
})

it('should create a parsable string using toString', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

const parsedStore = JSON.parse(store.toString())

expect(new Store(parsedStore)).toEqual(store)
})

it('should create a parsable string using toFormattedString', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

const parsedStore = JSON.parse(store.toFormattedString())

expect(new Store(parsedStore)).toEqual(store)
})

it('should create a parsable string using toMinimalFormattedString', () => {
const store = new Store(createStore({
linux: {
x64: ['10.0.0.0', '20.0.0.0', '30.0.0.0'],
x86: ['10.0.0.0']
}
}))

const parsedStore = JSON.parse(store.toMinimalFormattedString())

expect(new Store(parsedStore)).toEqual(store)
})

})
})
12 changes: 12 additions & 0 deletions store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class Store {
private store: ISetStore

public constructor(store_: IListStore) {

console.log('new: ')
console.log(store_)

this.store = {
linux: {
x64: new Set(store_.linux.x64),
Expand Down Expand Up @@ -113,6 +117,14 @@ export class Store {
return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)), null, spaces)
}

public toMinimalFormattedString(spaces = 4): string {
return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)), (k, v) => v instanceof Array ? JSON.stringify(v) : v, spaces)
.replace(/"\[/g, '[')
.replace(/\]"/g, ']')
.replace(/\\"/g, '"')
.replace(/""/g, '"')
}

public size(): StoreSize {
return {
linux: this.store.linux.x64.size + this.store.linux.x86.size,
Expand Down
8 changes: 4 additions & 4 deletions store/importStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(0)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toFormattedString(), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toMinimalFormattedString(), expect.any(Function))

})

Expand All @@ -93,7 +93,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(0)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toFormattedString(), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), anyStore.toMinimalFormattedString(), expect.any(Function))
})

it('should do nothing, if no store file is loaded from file system', async () => {
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), JSON.stringify(mergedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), new Store(mergedStore).toMinimalFormattedString(), expect.any(Function))
})

it('should merge the existing file with the file downloaded by local file', async () => {
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('importStore', () => {
expect(readFileMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), JSON.stringify(mergedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(pathJoin(__dirname, '..', LOCAL_STORE_FILE), new Store(mergedStore).toMinimalFormattedString(), expect.any(Function))
})
})
})
2 changes: 1 addition & 1 deletion store/importStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ export async function importAndMergeLocalstore(config: IStoreConfig): Promise<St
}

async function storeStoreFile(store: Store): Promise<void> {
return writeFilePromise(localStoreFilePath, store.toFormattedString())
return writeFilePromise(localStoreFilePath, store.toMinimalFormattedString())
}
12 changes: 6 additions & 6 deletions store/storeNegativeHit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ describe('storeNegativeHit', () => {
patch: 4,
}), 'linux', 'x64')

const expectedStore = createStore({ linux: { x64: ['1.2.3.4'], x86: [], } })
const expectedStore = new Store(createStore({ linux: { x64: ['1.2.3.4'], x86: [], } }))

expect(loadStoreMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(localPath, JSON.stringify(expectedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(localPath, expectedStore.toMinimalFormattedString(), expect.any(Function))
})

it('should extend an existing store with one entry', async () => {
Expand All @@ -81,12 +81,12 @@ describe('storeNegativeHit', () => {
patch: 4,
}), 'linux', 'x64')

const expectedStore = createStore({ linux: { x64: ['1.2.3.4'], x86: [] }, win: { x64: ['10.0.0.0'], x86: [], } })
const expectedStore = new Store(createStore({ linux: { x64: ['1.2.3.4'], x86: [] }, win: { x64: ['10.0.0.0'], x86: [], } }))

expect(loadStoreMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(localPath, JSON.stringify(expectedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(localPath, expectedStore.toMinimalFormattedString(), expect.any(Function))
})

it('should do nothing, if entry already exists', async () => {
Expand Down Expand Up @@ -128,11 +128,11 @@ describe('storeNegativeHit', () => {
patch: 4,
}), 'linux', 'x64')

const expectedStore = createStore({ linux: { x64: ['1.2.3.4'], x86: [] }, win: { x64: ['10.0.0.0', '11.0.0.0', '12.0.0.0'], x86: [], } })
const expectedStore = new Store(createStore({ linux: { x64: ['1.2.3.4'], x86: [] }, win: { x64: ['10.0.0.0', '11.0.0.0', '12.0.0.0'], x86: [], } }))

expect(loadStoreMock).toHaveBeenCalledTimes(1)

expect(writeFileMock).toHaveBeenCalledTimes(1)
expect(writeFileMock).toHaveBeenCalledWith(localPath, JSON.stringify(expectedStore, null, 4), expect.any(Function))
expect(writeFileMock).toHaveBeenCalledWith(localPath, expectedStore.toMinimalFormattedString(), expect.any(Function))
})
})
2 changes: 1 addition & 1 deletion store/storeNegativeHit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export async function storeNegativeHit(version: ComparableVersion, os: OS, arch:
if (!currentStore.has(os, arch, version)) {
currentStore.add(os, arch, version)

await writeFile(STORE_FILE, currentStore.toFormattedString())
await writeFile(STORE_FILE, currentStore.toMinimalFormattedString())
}
}
2 changes: 1 addition & 1 deletion utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IChromeConfig } from './interfaces/interfaces'
import type { OSSetting, OS, ExtendedOS } from './interfaces/os.interfaces'
import { IListStore, ISetStore } from './interfaces/store.interfaces'
import type { IListStore, ISetStore } from './interfaces/store.interfaces'

export function detectOperatingSystem(config: IChromeConfig): OSSetting {

Expand Down
4 changes: 3 additions & 1 deletion versions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { Store } from './store/Store'
import { storeNegativeHit } from './store/storeNegativeHit'
import { createNodeParserHTMLElement, createNodeWithChildren, createStore, createChromeSingleConfig, createChromeFullConfig } from './test/test.utils'
import { detectOperatingSystem } from './utils'
// eslint-disable-next-line import/no-namespace
import * as utils from './utils'
import { mapVersions, getChromeDownloadUrl, loadVersions } from './versions'

jest.mock('node-html-parser')
Expand All @@ -32,7 +34,7 @@ jest.mock('./store/storeNegativeHit')

// don't mock compareComparableVersions to test the sort and filtering based on version.comparableVersion
jest.mock('./utils', () => ({
...jest.requireActual<any>('./utils'),
...jest.requireActual<typeof utils>('./utils'),
detectOperatingSystem: jest.fn(),
}))

Expand Down

0 comments on commit f32a994

Please sign in to comment.