Skip to content

Commit

Permalink
Log view: unit test the selection of default file
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie committed Feb 15, 2024
1 parent 938ef41 commit 5898af0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
46 changes: 30 additions & 16 deletions src/views/Log.vue
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,40 @@ query LogFiles($id: ID!) {
}
`

// The preferred file to start with as a list of patterns
// The first pattern with a matching file name will be choosen
/**
* The preferred file to start with as a list of patterns.
* The first pattern with a matching file name will be chosen.
*/
const LOG_FILE_DEFAULTS = [
// job stdout
/job\.out/,
/^job\.out$/,
// job script (e.g. on job submission failure)
/job/,
/^job$/,
// scheduler log (lexographical sorting ensures the latest log)
/scheduler\/*/
/^scheduler\/*/
]

/**
* Return the default log file from the given log filenames, if there is a
* matching filename. Relies on the filenames having been sorted in descending
* order.
*
* @param {string[]} logFiles - list of available log filenames
* @returns {?string}
*/
export const getDefaultFile = (logFiles) => {
if (logFiles.length) {
for (const filePattern of LOG_FILE_DEFAULTS) {
for (const fileName of logFiles) {
if (filePattern.exec(fileName)) {
return fileName
}
}
}
}
return null // rather than undefined
}

class Results {
constructor () {
/** @type {string[]} */
Expand Down Expand Up @@ -410,18 +433,9 @@ export default {
if (this.file && !logFiles.includes(this.file)) {
this.file = null
}

if (!this.file) {
// set the default log file if appropriate
if (!this.file && logFiles.length) {
for (const filePattern of LOG_FILE_DEFAULTS) {
for (const fileName of logFiles) {
if (filePattern.exec(fileName)) {
this.file = fileName
break
}
}
if (this.file) { break }
}
this.file = getDefaultFile(logFiles)
}
}

Expand Down
48 changes: 47 additions & 1 deletion tests/unit/views/log.vue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,57 @@ import { createStore } from 'vuex'
import storeOptions from '@/store/options'
import { createVuetify } from 'vuetify'
import sinon from 'sinon'
import Log from '@/views/Log.vue'
import Log, { getDefaultFile } from '@/views/Log.vue'
import WorkflowService from '@/services/workflow.service'
import User from '@/model/User.model'
import { Tokens } from '@/utils/uid'

describe('getDefaultFile()', () => {
it.each([
{
files: [
'job.err',
'job.out',
'job',
'job-activity.log',
'job.status',
'zjob.out',
].sort().reverse(),
expected: 'job.out',
},
{
files: [
'job',
'job-activity.log',
'job.status',
'scheduler/pluto',
].sort().reverse(),
expected: 'job',
},
{
files: [
'scheduler/02-restart-02.log',
'scheduler/01-start-01.log',
'install/02-reinstall.log',
'config/flow-processed.cylc',
'config/20240212T155825+0000-rose-suite.conf',
'config/02-restart-02.cylc',
].sort().reverse(),
expected: 'scheduler/02-restart-02.log',
},
{
files: [],
expected: null,
},
{
files: ['ceres', 'vesta', 'aphosis'].sort().reverse(),
expected: null,
},
])('getDefaultFile($files) == $expected', ({ files, expected }) => {
expect(getDefaultFile(files)).toBe(expected)
})
})

describe('Log view', () => {
const owner = 'svimes'
const workflowName = 'thud'
Expand Down

0 comments on commit 5898af0

Please sign in to comment.