Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Jan 31, 2024
1 parent 73b5c78 commit 3540042
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
12 changes: 9 additions & 3 deletions extension/src/flow-cli/flow-version-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { Settings } from '../settings/settings'
import { execDefault } from '../utils/shell/exec'
import { StateCache } from '../utils/state-cache'
import * as semver from 'semver'
import { parseFlowCliVersion } from './utils'

const CHECK_FLOW_CLI_CMD = (flowCommand: string): string => `${flowCommand} version`

export class FlowVersionProvider {
#settings: Settings
#stateCache: StateCache<semver.SemVer | null>
#parseCliVersion: (buffer: Buffer | string) => string

constructor (settings: Settings) {
constructor (settings: Settings, parseCliVersion: (buffer: Buffer | string) => string = parseFlowCliVersion) {
this.#stateCache = new StateCache<semver.SemVer | null>(async () => await this.#fetchFlowVersion())
this.#settings = settings
this.#parseCliVersion = parseCliVersion
}

async #fetchFlowVersion (): Promise<semver.SemVer | null> {
Expand All @@ -23,7 +24,7 @@ export class FlowVersionProvider {
))).stdout

// Format version string from output
let versionStr: string | null = parseFlowCliVersion(buffer)
let versionStr: string | null = this.#parseCliVersion(buffer)

versionStr = semver.clean(versionStr)
if (versionStr === null) return null
Expand All @@ -50,3 +51,8 @@ export class FlowVersionProvider {
return this.#stateCache
}
}

export function parseFlowCliVersion (buffer: Buffer | string): string {
return (buffer.toString().split('\n')[0]).split(' ')[1]
}

3 changes: 0 additions & 3 deletions extension/src/flow-cli/utils.ts

This file was deleted.

30 changes: 15 additions & 15 deletions extension/test/integration/0 - dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ suite('Dependency Installer', () => {
})

test('Install Missing Dependencies', async () => {
const mocklanguageServerApi = {
const mockLanguageServerApi = {
activate: stub(),
deactivate: stub(),
isActive: true
}
const dependencyManager = new DependencyInstaller(mocklanguageServerApi as any, flowVersionProvider)
const dependencyManager = new DependencyInstaller(mockLanguageServerApi as any, flowVersionProvider)
await assert.doesNotReject(async () => { await dependencyManager.installMissing() })

// Check that all dependencies are installed
Expand All @@ -30,47 +30,47 @@ suite('Dependency Installer', () => {
}).timeout(MaxTimeout)

test('Flow CLI installer restarts langauge server if active', async () => {
const mocklanguageServerApi = {
const mockLanguageServerApi = {
activate: stub().callsFake(async () => {
mocklanguageServerApi.isActive = true
mockLanguageServerApi.isActive = true
}),
deactivate: stub().callsFake(async () => {
mocklanguageServerApi.isActive = false
mockLanguageServerApi.isActive = false
}),
isActive: true
}
const mockInstallerContext = {
refreshDependencies: async () => {},
languageServerApi: mocklanguageServerApi as any,
languageServerApi: mockLanguageServerApi as any,
flowVersionProvider
}
const flowCliInstaller = new InstallFlowCLI(mockInstallerContext)

await assert.doesNotReject(async () => { await flowCliInstaller.install() })
assert(mocklanguageServerApi.deactivate.calledOnce)
assert(mocklanguageServerApi.activate.calledOnce)
assert(mocklanguageServerApi.deactivate.calledBefore(mocklanguageServerApi.activate))
assert(mockLanguageServerApi.deactivate.calledOnce)
assert(mockLanguageServerApi.activate.calledOnce)
assert(mockLanguageServerApi.deactivate.calledBefore(mockLanguageServerApi.activate))
}).timeout(MaxTimeout)

test('Flow CLI installer does not restart langauge server if inactive', async () => {
const mocklanguageServerApi = {
const mockLanguageServerApi = {
activate: stub().callsFake(async () => {
mocklanguageServerApi.isActive = true
mockLanguageServerApi.isActive = true
}),
deactivate: stub().callsFake(async () => {
mocklanguageServerApi.isActive = false
mockLanguageServerApi.isActive = false
}),
isActive: false
}
const mockInstallerContext = {
refreshDependencies: async () => {},
languageServerApi: mocklanguageServerApi as any,
languageServerApi: mockLanguageServerApi as any,
flowVersionProvider
}
const flowCliInstaller = new InstallFlowCLI(mockInstallerContext)

await assert.doesNotReject(async () => { await flowCliInstaller.install() })
assert(mocklanguageServerApi.activate.notCalled)
assert(mocklanguageServerApi.deactivate.notCalled)
assert(mockLanguageServerApi.activate.notCalled)
assert(mockLanguageServerApi.deactivate.notCalled)
}).timeout(MaxTimeout)
})
20 changes: 18 additions & 2 deletions extension/test/unit/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { parseFlowCliVersion } from '../../src/flow-cli/utils'
import * as assert from 'assert'
import { parseFlowCliVersion } from '../../src/flow-cli/flow-version-provider'
import { execDefault } from '../../src/utils/shell/exec'
import { ASSERT_EQUAL } from '../globals'
import * as semver from 'semver'

suite('Parsing Unit Tests', () => {
test('Flow CLI Version Parsing', async () => {
test('Flow CLI Version Parsing Buffer', async () => {
let versionTest: Buffer = Buffer.from('Version: v0.1.0\nCommit: 0a1b2c3d')
let formatted = parseFlowCliVersion(versionTest)
ASSERT_EQUAL(formatted, 'v0.1.0')
Expand All @@ -11,4 +14,17 @@ suite('Parsing Unit Tests', () => {
formatted = parseFlowCliVersion(versionTest)
ASSERT_EQUAL(formatted, 'v0.1.0')
})

test('FLow CLI Version Parsing string', async () => {
let versionTest: string = 'Version: v0.1.0\nCommit: 0a1b2c3d'
let formatted = parseFlowCliVersion(versionTest)
ASSERT_EQUAL(formatted, 'v0.1.0')
})

test('Flow CLI Version Parsing Integration', async () => {
// Check that version is parsed from currently installed flow-cli
const {stdout} = await execDefault('flow version')
const formatted = parseFlowCliVersion(stdout)
assert(semver.valid(formatted))
})
})

0 comments on commit 3540042

Please sign in to comment.