Skip to content

Commit

Permalink
fix(fta): change path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
exiguus committed Feb 8, 2024
1 parent ec3250d commit 100f30b
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
id: test-action
uses: ./
with:
project_path: ../src/
project_path: ./src/

- name: Print Output Summary
id: output-summary
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ __tests__/runner/*
# fta
fta.config.json
output.json
tmp-config-file.json
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ jobs:

- name: FTA GitHub Action
id: fta-action
uses: exiguus/fta-github-action@v0.1.0
uses: exiguus/fta-github-action@v0.1.1
with:
config_path: fta.config.json

- name: Print Output Summary
id: output-summary
Expand All @@ -81,9 +83,9 @@ jobs:
Path to the project to analyze
**required**: true
**required**: false
**default**: "./src/"
**default**: "src/"
### config_path
Expand All @@ -97,7 +99,7 @@ Path to output file
**required**: false
**default**: "./output.json"
**default**: "output.json"
### format
Expand Down
7 changes: 0 additions & 7 deletions __tests__/fta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import * as fta from '../src/fta'
import { expect } from '@jest/globals'

describe('wait.ts', () => {
it('throws on invalid project_path', async () => {
const project_path = ''
await expect(fta.run(project_path)).rejects.toThrow(
'Param `project_path` does not exist'
)
})

it('throws on non-existent project_path', async () => {
const project_path = 'non-existent-file'
await expect(fta.run(project_path)).rejects.toThrow(
Expand Down
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ branding:
inputs:
project_path:
description: 'Path to the project to analyze'
required: true
default: './src/'
required: false
default: 'src/'
config_path:
description: 'Path to config file'
required: false
output_path:
description: 'Path to output file'
required: false
default: './output.json'
default: 'output.json'
format:
description:
'Output format (default: table) [default: table] [possible values: table,
Expand Down
2 changes: 1 addition & 1 deletion dist/fta/types.d.ts

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

27 changes: 14 additions & 13 deletions dist/index.js

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
"lint:fix": "npm run lint:fix:eslint && npm run lint:fix:markdownlint",
"package": "npx ncc build src/index.ts --license licenses.txt",
"package:watch": "npm run package -- --watch",
"test": "npm run test:unit && npm run test:output",
"test": "export GITHUB_WORKSPACE=$(pwd) && npm run test:unit && npm run test:output",
"test:unit": "npx jest",
"test:output": "node dist/index.js >/dev/null && diff output.json __mocks__/output.json || exit 1",
"test:output": "export GITHUB_WORKSPACE=$(pwd) && node dist/index.js >/dev/null && diff output.json __mocks__/output.json || exit 1",
"all": "npm run format && npm run lint && npm run test && npm run coverage && npm run package",
"prepare": "husky install"
},
Expand Down
2 changes: 1 addition & 1 deletion src/fta/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('writeConfig function', () => {
writeConfig(configPath, validConfigMock)

expect(fs.writeFileSync).toHaveBeenCalledWith(
path.join(__dirname, configPath),
path.join(process.env.GITHUB_WORKSPACE || '', configPath),
JSON.stringify(validConfigMock, null, 2),
{ encoding: 'utf8' }
)
Expand Down
7 changes: 5 additions & 2 deletions src/fta/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export const getConfig = (
): Partial<ActionOptions> | null => {
try {
const config = JSON.parse(
fs.readFileSync(path.join(__dirname, config_path), 'utf8')
fs.readFileSync(
path.join(process.env.GITHUB_WORKSPACE || '', config_path),
'utf8'
)
)
return isActionOptions(config) ? config : null
} catch (error) {
Expand All @@ -39,7 +42,7 @@ export const writeConfig = (
): void => {
try {
fs.writeFileSync(
path.join(__dirname, config_path),
path.join(process.env.GITHUB_WORKSPACE || '', config_path),
JSON.stringify(options, null, 2),
{ encoding: 'utf8' }
)
Expand Down
2 changes: 1 addition & 1 deletion src/fta/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActionInput, ActionOptions, Formats, OptionsMap } from './types'

export const defaultInput: ActionInput = {
filePath: '../src/',
projectPath: '/src/',
configPath: '',
outputPath: 'output.json',
format: 'json',
Expand Down
4 changes: 2 additions & 2 deletions src/fta/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import { writeOutput } from './output'

describe('writeOutput function', () => {
const outputFilePath = '../dist/test-output.json'
const outputFilePath = './dist/test-output.json'
const testData = '{"key": "value"}'

it('should write data to the specified output file', () => {
Expand All @@ -14,7 +14,7 @@ describe('writeOutput function', () => {

// Check if fs.writeFileSync was called with the expected arguments
expect(spyWriteFileSync).toHaveBeenCalledWith(
path.join(__dirname, '..', outputFilePath),
path.join(process.env.GITHUB_WORKSPACE || '', outputFilePath),
testData,
{ encoding: 'utf8' }
)
Expand Down
11 changes: 7 additions & 4 deletions src/fta/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import path from 'path'

export const writeOutput = (output_path: string, data: string): void => {
try {
// write output file into ../dist
fs.writeFileSync(path.join(__dirname, '..', output_path), data, {
encoding: 'utf8'
})
fs.writeFileSync(
path.join(process.env.GITHUB_WORKSPACE || '', output_path),
data,
{
encoding: 'utf8'
}
)
} catch (error) {
throw new Error('Error writing output file')
}
Expand Down
8 changes: 6 additions & 2 deletions src/fta/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ describe('run function', () => {

// Check if fs.existsSync and other functions were called with the expected arguments
expect(mockFsExistsSync).toHaveBeenCalledWith(
expect.stringContaining(path.join(__dirname, defaultInput.filePath))
expect.stringContaining(
path.join(process.env.GITHUB_WORKSPACE || '', defaultInput.projectPath)
)
)
expect(mockFsExistsSync).toHaveBeenCalledWith(
expect.stringContaining(defaultInput.configPath)
)
expect(mockFsExistsSync).toHaveBeenCalledWith(
expect.stringContaining(path.join(__dirname, defaultInput.filePath))
expect.stringContaining(
path.join(process.env.GITHUB_WORKSPACE || '', defaultInput.projectPath)
)
)

expect(mockWriteConfig).toHaveBeenCalledWith(
Expand Down
28 changes: 20 additions & 8 deletions src/fta/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function run(
options: Partial<ActionOptions> | null = null
): Promise<ActionOutput> {
if (!project_path) {
project_path = defaultInput.filePath
project_path = defaultInput.projectPath
}
if (!config_path) {
config_path = defaultInput.configPath
Expand All @@ -36,7 +36,9 @@ export async function run(
output_path = defaultInput.outputPath
}

if (!fs.existsSync(path.join(__dirname, project_path)))
if (
!fs.existsSync(path.join(process.env.GITHUB_WORKSPACE || '', project_path))
)
throw new Error('Param `project_path` does not exist')

// use --format over --json shorthand fta cli cmd
Expand All @@ -52,7 +54,7 @@ export async function run(
if (config_path.length > 0) {
// throw if config path does not exist
try {
fs.existsSync(path.join(__dirname, config_path))
fs.existsSync(path.join(process.env.GITHUB_WORKSPACE || '', config_path))
} catch (error) {
throw new Error('Param `config_path` does not exist')
}
Expand All @@ -70,7 +72,11 @@ export async function run(
throw new Error('Param `config_path` is not a json file')
}
try {
if (!fs.lstatSync(path.join(__dirname, config_path)).isFile())
if (
!fs
.lstatSync(path.join(process.env.GITHUB_WORKSPACE || '', config_path))
.isFile()
)
throw new Error('Param `config_path` is not a file')
} catch (error) {
throw new Error('Param `config_path` is not a file')
Expand Down Expand Up @@ -146,15 +152,21 @@ export async function run(
// output
// details is the output of the fta command with the format option
// details are also saved to a file in the github action
const configFile = path.join(__dirname, TMP_CONFIG_FILE)
const filePath = path.join(__dirname, project_path)
const configFile = path.join(
process.env.GITHUB_WORKSPACE || '',
TMP_CONFIG_FILE
)
const projectPath = path.join(
process.env.GITHUB_WORKSPACE || '',
project_path
)
const details = execSync(
`npm exec --package=fta-cli -c 'fta ${filePath} --config-path ${configFile} --format ${mappedOptions.format}'`
`npm exec --package=fta-cli -c 'fta ${projectPath} --config-path ${configFile} --format ${mappedOptions.format}'`
).toString()
// summary is the output of the fta command with the table format option
// to have a quick look at the results
const summary = execSync(
`npm exec --package=fta-cli -c 'fta ${filePath} --config-path ${configFile} --format table'`
`npm exec --package=fta-cli -c 'fta ${projectPath} --config-path ${configFile} --format table'`
).toString()

if (output_path) {
Expand Down
2 changes: 1 addition & 1 deletion src/fta/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type ActionOptions = Record<
>

export type ActionInput = {
filePath: string
projectPath: string
configPath: string
outputPath: string
} & Record<
Expand Down

0 comments on commit 100f30b

Please sign in to comment.