Skip to content

Commit

Permalink
feat(runner): installation setup
Browse files Browse the repository at this point in the history
when the required parameters are provided the runner performs
the installation in the configured directory by creating symlinks
in that folder pointing to the contents of `build-root/build_name`
  • Loading branch information
samugi committed Mar 26, 2024
1 parent cc7a087 commit 00ab547
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 15 deletions.
104 changes: 99 additions & 5 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions runner/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ inputs:
description:
'Name of the file into which to write test results in JUnit XML format'
required: false
setup-venv-path:
description: "Path to the script to source to set up Kong's virtual environment"
build-root:
description: "Path to the build root. This is where the script to source to set up Kong's virtual environment is found"
required: false
build-dest-path:
description: "Path to the build destination. Symlinks will be created in this folder pointing to the contents of `build-root/build_name`"
required: false

runs:
Expand Down
3 changes: 2 additions & 1 deletion src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ module.exports = {
core.getInput('failed-test-files-file', { required: true }),
core.getInput('test-file-runtime-file', { required: true }),
core.getInput('xml-output-file', { required: false }),
core.getInput('setup-venv-path', { required: false }),
core.getInput('build-root', { required: false }),
core.getInput('build-dest-path', { required: false }),
)
} catch (e) {
core.setFailed(e.message)
Expand Down
8 changes: 6 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ const cli = () => {
.argument('<kongDirectory>', 'Path to local Kong repository')
.argument('<xmlOutputFile>', 'XML output file')
.argument(
'<setupVenvPath>',
"Path to the script to source to set up Kong's virtual environment",
'<buildRootPath>',
"Path to the build root. This is where the script to source to set up Kong's virtual environment is found",
)
.argument(
'<buildDestPath>',
'Path to the build destination. Symlinks will be created in this folder pointing to the contents of `build-root/build_name`',
)
.action(runner)

Expand Down
79 changes: 79 additions & 0 deletions src/installation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const fs = require('fs').promises
const path = require('path')

/* Creates symlinks recursively, sub-directories that already exist in
* `sourceDir` are not overwritten, they are accessed and symlinks are created
* inside them for the corresponding files in the `destDir` tree. This is to
* avoid (as much as possible) to lose files from `sourceDir` by replacing
* non-empty directories with symlinks.
*/
const createSymlinks = async (sourceDir, destDir) => {
const filesCreated = []
const destFiles = await fs.readdir(destDir)

for (const destFile of destFiles) {
const sourceFilePath = path.join(sourceDir, destFile)
const destFilePath = path.join(destDir, destFile)

try {
const sourceStats = await fs.lstat(sourceFilePath)
if (sourceStats.isDirectory()) {
// if source file is a directory continue recursively
filesCreated.push(
...(await createSymlinks(sourceFilePath, destFilePath)),
)
continue
}
// delete source file if it already exists (and is not a directory)
await fs.unlink(sourceFilePath)
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(error)
}
}

// Create the symlink
await fs.symlink(destFilePath, sourceFilePath)
filesCreated.push(sourceFilePath)
}

return filesCreated
}

const setup = async (buildRootPath, buildName, sourceDirPath) => {
if (!buildRootPath || !buildName || !sourceDirPath) {
console.error(
'One or more required parameters are missing, skipping installation',
)
return
}
console.debug('Installation started')
const buildPath = path.join(buildRootPath, buildName)
const files = await createSymlinks(sourceDirPath, buildPath)
console.debug('Installation completed')
return files
}

const cleanup = async (files) => {
if (!files) {
console.log('No files to cleanup')
return
}

console.debug('Cleanup started')
for (const file of files) {
try {
await fs.unlink(file)
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(error)
}
}
}
console.debug('Cleanup completed')
}

module.exports = {
setup,
cleanup,
}
15 changes: 11 additions & 4 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { executeCommand } = require('./execute-command')
const appendToFile = require('./append-to-file')
const bustedEventListener = require('./busted-event-listener')
const { encodeJSON } = require('./encode-json')
const { setup, cleanup } = require('./installation')

const readTestsToRun = (testsToRunFile, failedTestFilesFile) => {
let file = testsToRunFile
Expand All @@ -29,7 +30,8 @@ const runner = async (
failedTestFilesFile,
testFileRuntimeFile,
xmlOutputFile,
setupVenvPath,
buildRootPath,
buildDestPath,
workingDirectory,
) => {
const testsToRun = readTestsToRun(testsToRunFile, failedTestFilesFile)
Expand Down Expand Up @@ -78,10 +80,14 @@ const runner = async (
},
)

let installedFiles = []
try {
const setupVenv = setupVenvPath
? `. ${setupVenvPath}/${venv_script} ;`
: ''
const build_name = venv_script ? venv_script.split('-venv')[0] : null
installedFiles = await setup(buildRootPath, build_name, buildDestPath)
const setupVenv =
buildRootPath && venv_script
? `. ${buildRootPath}/${venv_script} ;`
: ''
const excludeTagsOption = exclude_tags
? `--exclude-tags="${exclude_tags}"`
: ''
Expand Down Expand Up @@ -111,6 +117,7 @@ const runner = async (
console.error(error.message)
return false
} finally {
await cleanup(installedFiles)
listener.close()
}
}
Expand Down

0 comments on commit 00ab547

Please sign in to comment.