Skip to content

Commit bad3a2f

Browse files
danbot315Daniel Clayton
andauthored
test: expand CI core suite and restore context/config compatibility (#7)
Co-authored-by: Daniel Clayton <dan@Daniels-Mac-mini.local>
1 parent c0b79ad commit bad3a2f

5 files changed

Lines changed: 66 additions & 20 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ jobs:
3838
- name: Lint
3939
run: bun run lint
4040

41-
- name: Contract tests (workflow generator)
42-
run: bun run test -- packages/core/src/workflow-generator.test.ts
41+
- name: Core contract tests
42+
run: bun run test -- packages/core/src/workflow-generator.test.ts packages/core/src/context.test.ts packages/core/src/config.test.ts

packages/core/src/config.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import * as yaml from 'yaml';
44
import type { ConstructConfig, StackConfig } from './plugins/types.js';
5-
import type { DotGithubContext } from './context.js';
5+
import { DotGithubContext } from './context.js';
66

77
export interface DotGithubAction {
88
/** GitHub repository in org/repo format */
@@ -11,6 +11,8 @@ export interface DotGithubAction {
1111
ref: string;
1212
/** Original version reference that was requested (e.g., v4, main) */
1313
versionRef: string;
14+
/** Legacy function name (backward-compat) */
15+
functionName?: string;
1416
/** Action name for type names and function names (overrides default from YAML) */
1517
actionName?: string;
1618
/** Output file path where the TypeScript was generated - required when generateCode is true */
@@ -138,9 +140,12 @@ module.exports = ${JSON.stringify(config, null, 2)};
138140
/**
139141
* Sets a custom config file path to override the default discovery
140142
*/
143+
let customConfigPathOverride: string | undefined;
144+
141145
export function setConfigPath(configPath: string): void {
142-
// This function is kept for compatibility but doesn't need to do anything
143-
// since we're using DotGithubContext now
146+
customConfigPathOverride = configPath
147+
? path.resolve(configPath)
148+
: undefined;
144149
}
145150

146151
/**
@@ -189,6 +194,25 @@ export function getConfigPath(): string {
189194
'dotgithub.yml',
190195
];
191196

197+
if (customConfigPathOverride) {
198+
const stat = fs.existsSync(customConfigPathOverride)
199+
? fs.statSync(customConfigPathOverride)
200+
: undefined;
201+
202+
if (stat?.isDirectory()) {
203+
const githubDir = path.join(customConfigPathOverride, '.github');
204+
for (const fileName of CONFIG_FILE_NAMES) {
205+
const configPath = path.join(githubDir, fileName);
206+
if (fs.existsSync(configPath)) {
207+
return configPath;
208+
}
209+
}
210+
return path.join(githubDir, CONFIG_FILE_NAMES[0]!);
211+
}
212+
213+
return customConfigPathOverride;
214+
}
215+
192216
let currentDir = process.cwd();
193217

194218
while (currentDir !== path.dirname(currentDir)) {
@@ -310,8 +334,23 @@ export function writeConfig(
310334
*/
311335
export function addActionToConfig(
312336
actionInfo: DotGithubAction,
313-
context: DotGithubContext
337+
contextOrRootDir: DotGithubContext | string
314338
): void {
339+
const context: DotGithubContext =
340+
typeof contextOrRootDir === 'string'
341+
? new DotGithubContext({
342+
config: { ...readConfig(), rootDir: contextOrRootDir },
343+
configPath: getConfigPath(),
344+
})
345+
: contextOrRootDir;
346+
347+
if (!context.config) {
348+
context.config = createDefaultConfig();
349+
}
350+
if (!context.config.actions) {
351+
context.config.actions = [];
352+
}
353+
315354
// Check if action already exists (check orgRepo AND actionPath for uniqueness)
316355
const existingIndex = context.config.actions.findIndex(
317356
(action) =>
@@ -323,6 +362,10 @@ export function addActionToConfig(
323362
...actionInfo,
324363
};
325364

365+
if (actionWithRelativePath.outputPath && path.isAbsolute(actionWithRelativePath.outputPath)) {
366+
actionWithRelativePath.outputPath = context.relativePath(actionWithRelativePath.outputPath);
367+
}
368+
326369
if (existingIndex >= 0) {
327370
// Update existing action
328371
context.config.actions[existingIndex] = actionWithRelativePath;

packages/core/src/constructs/workflow.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export class WorkflowConstruct extends Construct {
1212

1313
this._workflow = workflow;
1414

15-
scope.addWorkflow(id, this._workflow);
15+
if (typeof (scope as any).addWorkflow === 'function') {
16+
(scope as any).addWorkflow(id, this._workflow);
17+
}
1618
}
1719

1820
addJob(id: string, jobConstruct: JobConstruct): JobConstruct {

packages/core/src/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ export class DotGithubContext {
1111
config: DotGithubConfig;
1212
configPath: string;
1313
rootPath: string;
14+
outputPath: string;
1415

1516
constructor({ config, configPath }: DotGithubContextOptions) {
1617
this.config = config;
1718
this.configPath = configPath;
18-
this.rootPath = path.join(path.dirname(configPath), this.config.rootDir);
19+
const rootDir = this.config.rootDir ?? this.config.outputDir ?? 'src';
20+
this.rootPath = path.join(path.dirname(configPath), rootDir);
21+
this.outputPath = this.rootPath;
1922
}
2023

2124
resolvePath(relativePath: string): string {

packages/core/src/plugin-generator.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
PropertyDeclaration,
1414
} from 'ts-morph';
1515
import { generateActionFiles } from './actions-manager.js';
16+
import { generateFunctionName } from './utils.js';
1617

1718
// Enhanced TypeScript interfaces for better type safety
1819
interface WorkflowSchema {
@@ -207,7 +208,7 @@ export async function createConstructFromFiles(
207208
}
208209

209210
// Check if there's a local dotgithub.json config in the source directory
210-
let configToUse = context.config;
211+
let configToUse: any = context.config || {};
211212
const localConfigPath = path.join(githubFilesPath, 'dotgithub.json');
212213
if (fs.existsSync(localConfigPath)) {
213214
try {
@@ -226,13 +227,10 @@ export async function createConstructFromFiles(
226227
// Generate construct content (now with updated config that includes auto-added actions)
227228
// Filter actions to only include those with functionName for code generation
228229
const configForConstruct: Config = {
229-
actions: configToUse.actions
230-
.filter((action) => action.generateCode !== false && action.outputPath)
231-
.map((action) => {
232-
const { generateFunctionName } = require('./utils');
233-
const functionName = action.actionName
234-
? generateFunctionName(action.actionName)
235-
: action.orgRepo;
230+
actions: (configToUse.actions || [])
231+
.filter((action: any) => action.generateCode !== false && action.outputPath)
232+
.map((action: any) => {
233+
const functionName = generateFunctionName(action.actionName || action.orgRepo);
236234
return {
237235
orgRepo: action.orgRepo,
238236
ref: action.ref,
@@ -299,13 +297,13 @@ export async function generateConstructFromGitHubFiles(
299297
const { content, filename } = await downloadGitHubFile(source);
300298
// Filter actions to only include those with outputPath for code generation
301299
const configForConstruct: Config = {
302-
actions: context.config.actions
303-
.filter((action) => action.generateCode !== false && action.outputPath)
304-
.map((action) => {
300+
actions: (context.config.actions || [])
301+
.filter((action: any) => action.generateCode !== false && action.outputPath)
302+
.map((action: any) => {
305303
const { generateFunctionName } = require('./utils');
306304
const functionName = action.actionName
307305
? generateFunctionName(action.actionName)
308-
: action.orgRepo;
306+
: generateFunctionName(action.actionName || action.orgRepo);
309307
return {
310308
orgRepo: action.orgRepo,
311309
ref: action.ref,

0 commit comments

Comments
 (0)