Skip to content

Commit

Permalink
Fix ability to use add-track force
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed May 12, 2021
1 parent 6a2059b commit 0f2ae2e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 93 deletions.
118 changes: 49 additions & 69 deletions products/jbrowse-cli/src/commands/add-track.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,27 @@ import { setup } from '../testUtil'

const fsPromises = fs.promises

const simpleBam = path.join(__dirname, '..', '..', 'test', 'data', 'simple.bam')
const simpleBai = path.join(__dirname, '..', '..', 'test', 'data', 'simple.bai')
const testConfig = path.join(
__dirname,
'..',
'..',
'test',
'data',
'test_config.json',
)
const baseDir = path.join(__dirname, '..', '..', 'test', 'data')
const simpleBam = path.join(baseDir, 'simple.bam')
const simpleBai = path.join(baseDir, 'simple.bai')
const testConfig = path.join(baseDir, 'test_config.json')

async function initctx(ctx: { dir: string }) {
await fsPromises.copyFile(
testConfig,
path.join(ctx.dir, path.basename(testConfig)),
)

await fsPromises.rename(
path.join(ctx.dir, path.basename(testConfig)),
path.join(ctx.dir, 'config.json'),
)
await fsPromises.copyFile(testConfig, path.join(ctx.dir, 'config.json'))
}
async function init2bit(ctx: { dir: string }) {
const simple2bit = path.join(
__dirname,
'..',
'..',
'test',
'data',
'simple.2bit',
)
await fsPromises.copyFile(
simple2bit,
path.join(ctx.dir, path.basename(simple2bit)),
path.join(baseDir, 'simple.2bit'),
path.join(ctx.dir, 'simple.2bit'),
)
}

async function readConf(ctx: { dir: string }) {
return fsPromises.readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
}

describe('add-track', () => {
setup.command(['add-track']).exit(2).it('fails if no track is specified')
setup
Expand All @@ -67,11 +51,36 @@ describe('add-track', () => {
.command(['add-track', simpleBam, '--load', 'copy'])
.command(['add-track', simpleBam, '--load', 'copy'])
.exit(160)
.it('Cannot add a track with the same track id')
.it('cannot add a track with the same track id')

setup
.do(initctx)
.command(['add-track', simpleBam, '--load', 'symlink'])
.command(['add-track', simpleBam, '--load', 'symlink', '--force'])
.it('use force to overwrite a symlink')

setup
.do(initctx)
.command(['add-track', simpleBam, '--load', 'copy'])
.command(['add-track', simpleBam, '--load', 'copy', '--force'])
.it('use force to overwrite a copied file')

// setting up a test for move difficult currently, because it would literally
// move the file in our test data...
// setup
// .do(initctx)
// .do(async ctx => {
// await fsPromises.copyFile(simpleBam, path.join(ctx.dir, 'new.bam'))
// await fsPromises.copyFile(simpleBai, path.join(ctx.dir, 'new.bam.bai'))
// })
// .command(['add-track', 'new.bam', '--load', 'move'])
// .command(['add-track', 'new.bam', '--load', 'move', '--force'])
// .it('use force to overwrite a moved file')

setup
.command(['add-track', simpleBam, '--load', 'copy'])
.catch(/no such file or directory/)
.it('Cannot add a track if there is no config file')
.it('cannot add a track if there is no config file')
setup
.do(initctx)
.do(ctx => {
Expand All @@ -88,11 +97,7 @@ describe('add-track', () => {
.do(initctx)
.command(['add-track', simpleBam, '--load', 'copy'])
.it('adds a track', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)

const contents = await readConf(ctx)
expect(fs.existsSync(path.join(ctx.dir, 'simple.bam'))).toBeTruthy()
expect(fs.existsSync(path.join(ctx.dir, 'simple.bam.bai'))).toBeTruthy()

Expand Down Expand Up @@ -128,10 +133,7 @@ describe('add-track', () => {
.do(initctx)
.command(['add-track', '/testing/in/place.bam', '--load', 'inPlace'])
.it('adds a track with load inPlace', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)

expect(JSON.parse(contents).tracks).toEqual([
{
Expand Down Expand Up @@ -171,10 +173,7 @@ describe('add-track', () => {
'/something/else/random.bai',
])
.it('adds a track with load inPlace', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)

expect(JSON.parse(contents).tracks).toEqual([
{
Expand Down Expand Up @@ -215,11 +214,7 @@ describe('add-track', () => {
simpleBai,
])
.it('adds a track', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)

const contents = await readConf(ctx)
expect(fs.existsSync(path.join(ctx.dir, 'simple.bam'))).toBeTruthy()
expect(fs.existsSync(path.join(ctx.dir, 'simple.bai'))).toBeTruthy()

Expand Down Expand Up @@ -255,10 +250,7 @@ describe('add-track', () => {
.do(initctx)
.command(['add-track', simpleBam, '--load', 'copy', '--subDir', 'bam'])
.it('adds a track with subDir', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)
expect(JSON.parse(contents).tracks).toEqual([
{
type: 'AlignmentsTrack',
Expand Down Expand Up @@ -300,10 +292,7 @@ describe('add-track', () => {
'bam',
])
.it('adds a track with subDir', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)
expect(JSON.parse(contents).tracks).toEqual([
{
type: 'AlignmentsTrack',
Expand Down Expand Up @@ -355,10 +344,7 @@ describe('add-track', () => {
'{"defaultRendering": "test"}',
])
.it('adds a track with all the custom fields', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)
expect(JSON.parse(contents).tracks).toEqual([
{
type: 'CustomTrackType',
Expand Down Expand Up @@ -388,10 +374,7 @@ describe('add-track', () => {
.do(initctx)
.command(['add-track', 'https://mysite.com/data/simple.bam'])
.it('adds a track from a url', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)
expect(JSON.parse(contents).tracks).toEqual([
{
type: 'AlignmentsTrack',
Expand Down Expand Up @@ -445,10 +428,7 @@ describe('add-track', () => {
'testAssembly',
])
.it('adds a track to a config with multiple assemblies', async ctx => {
const contents = await fsPromises.readFile(
path.join(ctx.dir, 'config.json'),
{ encoding: 'utf8' },
)
const contents = await readConf(ctx)
expect(JSON.parse(contents).tracks).toEqual([
{
type: 'AlignmentsTrack',
Expand Down
51 changes: 27 additions & 24 deletions products/jbrowse-cli/src/commands/add-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,53 +292,56 @@ export default class AddTrack extends JBrowseCommand {
}

// copy/symlinks/moves the track into the jbrowse installation directory
const filePaths = Object.values(this.guessFileNames(location, index))
const filePaths = Object.values(
this.guessFileNames(location, index),
).filter(f => !!f) as string[]

const destinationFn = (dir: string, file: string) =>
path.join(dir, subDir, path.basename(file))

switch (load) {
case 'copy': {
await Promise.all(
filePaths.map(async filePath => {
if (!filePath) {
return undefined
const dest = destinationFn(configDirectory, filePath)
try {
if (force) await fsPromises.unlink(dest)
} catch (e) {
this.error(e)
}
const dataLocation = path.join(
configDirectory,
subDir,
path.basename(filePath),
return fsPromises.copyFile(
filePath,
dest,
fs.constants.COPYFILE_EXCL,
)
return fsPromises.copyFile(filePath, dataLocation)
}),
)
break
}
case 'symlink': {
await Promise.all(
filePaths.map(async filePath => {
if (!filePath) {
return undefined
const dest = destinationFn(configDirectory, filePath)
try {
if (force) await fsPromises.unlink(dest)
} catch (e) {
this.error(e)
}
const dataLocation = path.join(
configDirectory,
subDir,
path.basename(filePath),
)
return fsPromises.symlink(filePath, dataLocation)
return fsPromises.symlink(filePath, dest)
}),
)
break
}
case 'move': {
await Promise.all(
filePaths.map(async filePath => {
if (!filePath) {
return undefined
const dest = destinationFn(configDirectory, filePath)
try {
if (force) await fsPromises.unlink(dest)
} catch (e) {
this.error(e)
}
const dataLocation = path.join(
configDirectory,
subDir,
path.basename(filePath),
)
return fsPromises.rename(filePath, dataLocation)
return fsPromises.rename(filePath, dest)
}),
)
break
Expand Down

0 comments on commit 0f2ae2e

Please sign in to comment.