Skip to content

Commit

Permalink
feat: implement current bucket tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyguenther committed Mar 6, 2024
1 parent 75d9121 commit b2f3409
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
57 changes: 57 additions & 0 deletions src/services/bucket/current.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { fileExists, inTemporaryDirectory, mkdir, writeFile } from '@shopify/cli-kit/node/fs'
import { joinPath } from '@shopify/cli-kit/node/path'
import { renderInfo } from '@shopify/cli-kit/node/ui'
import { describe, expect, test, vi } from 'vitest'
import { current } from './current.js'

vi.mock('@shopify/cli-kit/node/ui')

describe('current', () => {
test('gets current bucket with 0.x current file', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const shopkeeperRoot = joinPath(tmpDir, '.shopkeeper')
const legacyCurrentFile = joinPath(shopkeeperRoot, '.current-store')
const newCurrentFile = joinPath(shopkeeperRoot, '.current-bucket')
await mkdir(shopkeeperRoot)
await writeFile(legacyCurrentFile, 'production \n')

// When
await current(shopkeeperRoot)

// Then
const newCurrentFileExist = await fileExists(newCurrentFile)
expect(newCurrentFileExist).toBe(true)

const legacyCurrentFileExists = await fileExists(legacyCurrentFile)
expect(legacyCurrentFileExists).toBe(false)

expect(renderInfo).toHaveBeenCalledWith({
headline: 'Current bucket',
body: 'production is selected',
})
})
})

test('gets current bucket with 1.x current file', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const shopkeeperRoot = joinPath(tmpDir, '.shopkeeper')
const newCurrentFile = joinPath(shopkeeperRoot, '.current-bucket')
await mkdir(shopkeeperRoot)
await writeFile(newCurrentFile, 'production \n')

// When
await current(shopkeeperRoot)

// Then
const newCurrentFileExist = await fileExists(newCurrentFile)
expect(newCurrentFileExist).toBe(true)

expect(renderInfo).toHaveBeenCalledWith({
headline: 'Current bucket',
body: 'production is selected',
})
})
})
})
24 changes: 6 additions & 18 deletions src/services/bucket/current.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import {fileExistsSync, readFile, renameFile} from '@shopify/cli-kit/node/fs'
import {renderInfo} from '@shopify/cli-kit/node/ui'
import {getShopkeeperPath} from '../../utilities/bucket.js'
import {CURRENT_BUCKET_FILE, LEGACY_CURRENT_BUCKET_FILE} from '../../utilities/constants.js'
import {joinPath} from '@shopify/cli-kit/node/path'
import { renderInfo } from '@shopify/cli-kit/node/ui'
import { getCurrentBucket, getShopkeeperPath } from '../../utilities/bucket.js'

export async function current() {
const shopkeeperRoot = await getShopkeeperPath()

let currentBucket: string
const legacyBucketPath = joinPath(shopkeeperRoot, LEGACY_CURRENT_BUCKET_FILE)
const currentBucketPath = joinPath(shopkeeperRoot, CURRENT_BUCKET_FILE)
if (fileExistsSync(legacyBucketPath)) {
currentBucket = await readFile(legacyBucketPath)
await renameFile(legacyBucketPath, currentBucketPath)
} else {
currentBucket = await readFile(currentBucketPath)
}
export async function current(rootPath: string | null = null) {
const shopkeeperRoot = rootPath || await getShopkeeperPath()
const currentBucket = await getCurrentBucket(shopkeeperRoot)

renderInfo({
headline: 'Current bucket',
body: `${currentBucket.trim()} is selected`,
body: `${currentBucket} is selected`,
})
}

0 comments on commit b2f3409

Please sign in to comment.