Skip to content

Commit

Permalink
feat: implement init service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyguenther committed Mar 6, 2024
1 parent 3a35639 commit 715d8cc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 11 deletions.
85 changes: 85 additions & 0 deletions src/services/bucket/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { inTemporaryDirectory, readFile, writeFile } from '@shopify/cli-kit/node/fs'
import { joinPath } from '@shopify/cli-kit/node/path'
import { describe, expect, test, vi } from 'vitest'
import { DEFAULT_GITIGNORE_FILE, init } from './init.js'
import { expectFileToExist, expectFileToHaveContent } from '../../utilities/test-helpers.js'
import { renderSuccess } from '@shopify/cli-kit/node/ui'

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

describe('init', () => {
describe('init', () => {
describe('when .gitignore exists', () => {
test('creates buckets and updates .gitignore', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const bucketName = 'production'
const shopkeeperRoot = joinPath(tmpDir, '.shopkeeper')
const gitignorePath = joinPath(tmpDir, '.gitignore')
await writeFile(gitignorePath, '# Content #')

// When
await init([bucketName], tmpDir)

// Then
expectFileToExist(shopkeeperRoot)
expectFileToHaveContent(gitignorePath, DEFAULT_GITIGNORE_FILE)
expect(renderSuccess).toHaveBeenCalledWith({
headline: 'Shopkeeper initialized',
body: {
list: {
items: ['.shopkeeper directory created', '.gitignore updated']
},
},
customSections: [
{
title: 'Buckets created:',
body: {
list: {
items: ['production'],
},
},
},
]
})
})
})
})

describe('when .gitignore does not exist', () => {
test('creates buckets and creates .gitignore', async () => {
await inTemporaryDirectory(async (tmpDir: string) => {
// Given
const bucketName = 'production'
const shopkeeperRoot = joinPath(tmpDir, '.shopkeeper')
const gitignorePath = joinPath(tmpDir, '.gitignore')

// When
await init([bucketName], tmpDir)

// Then
expectFileToExist(shopkeeperRoot)
expectFileToHaveContent(gitignorePath, DEFAULT_GITIGNORE_FILE)
expect(renderSuccess).toHaveBeenCalledWith({
headline: 'Shopkeeper initialized',
body: {
list: {
items: ['.shopkeeper directory created', '.gitignore updated']
},
},
customSections: [
{
title: 'Buckets created:',
body: {
list: {
items: ['production'],
},
},
},
]
})
})
})
})
})
})
24 changes: 13 additions & 11 deletions src/services/bucket/init.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import {appendFile, fileExists, mkdir, writeFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {renderSuccess} from '@shopify/cli-kit/node/ui'
import {createBuckets} from '../../utilities/bucket.js'
import {SHOPKEEPER_DIRECTORY} from '../../utilities/constants.js'
import { appendFile, fileExists, mkdir, writeFile } from '@shopify/cli-kit/node/fs'
import { joinPath } from '@shopify/cli-kit/node/path'
import { renderSuccess } from '@shopify/cli-kit/node/ui'
import { createBuckets, getShopkeeperPath } from '../../utilities/bucket.js'
import { SHOPKEEPER_DIRECTORY } from '../../utilities/constants.js'

export async function init(buckets: string[]) {
await mkdir(joinPath('.', SHOPKEEPER_DIRECTORY))
export async function init(buckets: string[], cwd?: string) {
const projectRoot = cwd || '.'
await mkdir(joinPath(projectRoot, SHOPKEEPER_DIRECTORY))
const shopkeeperRoot = await getShopkeeperPath(projectRoot)

let action = ''
const gitIgnorePath = joinPath('.', '.gitignore')
const gitIgnorePath = joinPath(projectRoot, '.gitignore')
if (await fileExists(gitIgnorePath)) {
await appendFile(gitIgnorePath, DEFAULT_GITIGNORE_FILE)
await appendFile(gitIgnorePath, '\n\n' + DEFAULT_GITIGNORE_FILE)
action = 'updated'
} else {
await writeFile(gitIgnorePath, DEFAULT_GITIGNORE_FILE)
action = 'created'
}

await createBuckets(buckets)
await createBuckets(shopkeeperRoot, buckets)

renderSuccess({
headline: 'Shopkeeper initialized',
Expand Down Expand Up @@ -47,7 +49,7 @@ function renderBucketListSection(buckets: string[]) {
}
}

const DEFAULT_GITIGNORE_FILE = `# Shopkeeper #
export const DEFAULT_GITIGNORE_FILE = `# Shopkeeper #
##########
.shopkeeper/**/**/.env
.shopkeeper/.current-bucket
Expand Down

0 comments on commit 715d8cc

Please sign in to comment.