Skip to content

Commit

Permalink
fix: fix config resolution (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
oviecodes committed Sep 14, 2023
1 parent 4ad330d commit aaa489f
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 58 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/anime-http/server/auth/trendingAnimeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function serverAuth({ authProps, done }) {
console.log("token", authProps.getToken())
console.log("userpassword", authProps.getUserPass())

done(false)
done(true)
}

export async function clientAuth({ serverName }) {
Expand Down
43 changes: 16 additions & 27 deletions examples/kafka-test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions src/lib/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { logErrorLine, logWarningMessage } from './logger.js'

interface Config {
functionsDir?: string
lifecycleDir?: string
authDir?: string
}

let GLEE_DIR: string
Expand All @@ -29,9 +31,9 @@ export async function initializeConfigs(
)
GLEE_FUNCTIONS_DIR = path.resolve(
GLEE_DIR,
config.functionsDir || 'functions'
config.lifecycleDir || 'functions'
)
GLEE_AUTH_DIR = path.resolve(GLEE_DIR, config.functionsDir || 'auth')
GLEE_AUTH_DIR = path.resolve(GLEE_DIR, config.authDir || 'auth')

GLEE_CONFIG_FILE_PATH_TS = path.resolve(GLEE_DIR, 'glee.config.ts')
GLEE_CONFIG_FILE_PATH_JS = path.resolve(GLEE_DIR, 'glee.config.js')
Expand Down Expand Up @@ -76,7 +78,7 @@ function isFileReadable(filePath: string) {
/**
* Loads the configuration from glee project.
*/
async function loadConfigsFromFile() {
export async function loadConfigsFromFile() {
if (!isFileReadable(GLEE_CONFIG_FILE_PATH)) return
try {
let { default: projectConfigs } = await import(
Expand All @@ -87,10 +89,18 @@ async function loadConfigsFromFile() {
}
if (!projectConfigs) return

GLEE_DIR = projectConfigs.glee?.gleeDir || GLEE_DIR
GLEE_LIFECYCLE_DIR = projectConfigs.glee?.lifecycleDir ?? GLEE_LIFECYCLE_DIR
GLEE_FUNCTIONS_DIR = projectConfigs.glee?.functionsDir ?? GLEE_FUNCTIONS_DIR
GLEE_AUTH_DIR = projectConfigs.glee?.authDir ?? GLEE_AUTH_DIR
GLEE_DIR = projectConfigs.glee?.gleeDir
? path.resolve(GLEE_PROJECT_DIR, projectConfigs.glee?.gleeDir)
: GLEE_DIR
GLEE_FUNCTIONS_DIR = projectConfigs.glee?.functionsDir
? path.resolve(GLEE_DIR, projectConfigs.glee?.functionsDir)
: GLEE_FUNCTIONS_DIR
GLEE_LIFECYCLE_DIR = projectConfigs.glee?.lifecycleDir
? path.resolve(GLEE_DIR, projectConfigs.glee?.lifecycleDir)
: GLEE_LIFECYCLE_DIR
GLEE_AUTH_DIR = projectConfigs.glee?.authDir
? path.resolve(GLEE_DIR, projectConfigs.glee?.authDir)
: GLEE_AUTH_DIR
ASYNCAPI_FILE_PATH =
projectConfigs.glee?.asyncapiFilePath ?? ASYNCAPI_FILE_PATH
return projectConfigs
Expand Down
145 changes: 122 additions & 23 deletions test/lib/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,133 @@
import fs from 'fs'
import path from 'path'
import { findSpecFile } from '../../src/lib/configs.js'
import {
findSpecFile,
loadConfigsFromFile,
initializeConfigs,
getConfigs,
} from '../../src/lib/configs.js'

const yamlPath = path.resolve('./asyncapi.yaml')
const jsonPath = path.resolve('./asyncapi.json')

const gleePath = './.glee'
const configFile = '/glee.config.js'
const configsPath = path.join(gleePath, configFile)
const [lifecycleDir, functionDir, authDir] = ['./lifecy', './func', './authSec']
// const cwd = process.cwd()

describe('Tests resolving the AsyncAPI file path.', () => {
afterEach(async () => {
const promises = [jsonPath, yamlPath].map(async (file) => fs.unlinkSync(file))
await Promise.allSettled(promises)
})
afterEach(async () => {
const promises = [jsonPath, yamlPath].map(async (file) =>
fs.unlinkSync(file)
)
await Promise.allSettled(promises)
})

test('Should return undefined if AsyncAPI file doesn\'t exists.', async () => {
expect(findSpecFile("")).toBe(undefined)
})
test("Should return undefined if AsyncAPI file doesn't exists.", async () => {
expect(findSpecFile('')).toBe(undefined)
})

test('Should return undefined if there are multiple AsyncAPI spec files.', async () => {
fs.writeFileSync(jsonPath, '')
fs.writeFileSync(yamlPath, '')
expect(findSpecFile("")).toBe(undefined)
})
test('Should return undefined if there are multiple AsyncAPI spec files.', async () => {
fs.writeFileSync(jsonPath, '')
fs.writeFileSync(yamlPath, '')
expect(findSpecFile('')).toBe(undefined)
})

test('Should fails if asyncapi.json is a folder.', async () => {
fs.mkdirSync(jsonPath)
expect(findSpecFile("")).toBe(undefined)
fs.rmdirSync(jsonPath)
})
test('Should fails if asyncapi.json is a folder.', async () => {
fs.mkdirSync(jsonPath)
expect(findSpecFile('')).toBe(undefined)
fs.rmdirSync(jsonPath)
})

test('Should succeed in finding AsyncAPI spec if there is only one.', async () => {
fs.writeFileSync(jsonPath, '')
const resultingPath = findSpecFile('')
expect(resultingPath).toBe(jsonPath)
})
})

describe('Test resolving the config file path when no config is initialized', () => {
beforeEach(async () => {})

test('getConfigs function should return undefined for configs', () => {
expect(getConfigs().GLEE_CONFIG_FILE_PATH).toBe(undefined)
expect(getConfigs().ASYNCAPI_FILE_PATH).toBe(undefined)
})

test('getConfigs function should return undefined for config file properties', () => {
expect(getConfigs().GLEE_LIFECYCLE_DIR).toBeUndefined()
expect(getConfigs().GLEE_FUNCTIONS_DIR).toBeUndefined()
expect(getConfigs().GLEE_AUTH_DIR).toBeUndefined()
})

test('Should succeed in finding AsyncAPI spec if there is only one.', async () => {
fs.writeFileSync(jsonPath, '')
const resultingPath = findSpecFile('')
expect(resultingPath).toBe(jsonPath)
test('if no config file is found return undefined', async () => {
expect(await loadConfigsFromFile()).toBeUndefined()
})
})

describe('check config features when config is initialized', () => {
beforeEach(async () => {
fs.writeFileSync(yamlPath, '')
const conf = `export default async function () {
return {
glee: { // Glee core configurations
lifecycleDir: '${lifecycleDir}',
functionsDir: '${functionDir}',
authDir: '${authDir}'
}
}
}`
fs.mkdirSync(gleePath)
fs.writeFileSync(configsPath, conf)
await initializeConfigs()
})

afterEach(async () => {
const promises = [configsPath, yamlPath].map(async (file) =>
fs.unlinkSync(file)
)
await Promise.allSettled(promises)
fs.rmSync(gleePath, {
recursive: true,
force: true,
})
})
})

test('loadConfigFromFile should return defined value when there is a config file', async () => {
expect(await loadConfigsFromFile()).toBeDefined()
})

test('Should read config data from the current working directory', () => {
console.log('config_file_path', getConfigs())
expect(getConfigs().GLEE_PROJECT_DIR).toBe(process.cwd())
})

test('expect glee dir to be /.glee in current working directory', () => {
expect(getConfigs().GLEE_DIR).toBe(path.join(process.cwd(), gleePath))
})

test('GLEE_CONFIG_FILE_PATH should be glee.config.js', () => {
expect(getConfigs().GLEE_CONFIG_FILE_PATH).toBe(
path.join(process.cwd(), configsPath)
)
})

test('lifecycle folder should be same as set in config file', () => {
expect(getConfigs().GLEE_LIFECYCLE_DIR).toBe(
path.join(process.cwd(), gleePath, lifecycleDir)
)
})

test('function folder should be same as set in config file', () => {
expect(getConfigs().GLEE_FUNCTIONS_DIR).toBe(
path.join(process.cwd(), gleePath, functionDir)
)
})

test('Auth folder should be same as set in config file', () => {
expect(getConfigs().GLEE_AUTH_DIR).toBe(
path.join(process.cwd(), gleePath, authDir)
)
})
})

0 comments on commit aaa489f

Please sign in to comment.