Skip to content

Commit

Permalink
More use of helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 1, 2022
1 parent 1512507 commit 42c4632
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 240 deletions.
150 changes: 40 additions & 110 deletions products/jbrowse-cli/src/commands/add-assembly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import fs from 'fs'
import * as path from 'path'

import { setup } from '../testUtil'
import { setup, ctxDir, dataDir, readConf } from '../testUtil'

const { rename, copyFile, readdir, readFile, writeFile, mkdir } = fs.promises
const { rename, copyFile, writeFile, mkdir } = fs.promises

const defaultConfig = {
assemblies: [],
Expand Down Expand Up @@ -140,11 +140,8 @@ describe('add-assembly', () => {
})
.command(['add-assembly', 'simple.fasta', '--load', 'copy'])
.it('adds an assembly from a FASTA', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -193,11 +190,8 @@ describe('add-assembly', () => {
})
.command(['add-assembly', 'simple.fa', '--load', 'copy'])
.it('adds an assembly from a FASTA (.fa extension)', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -246,11 +240,8 @@ describe('add-assembly', () => {
})
.command(['add-assembly', 'simple.fasta.gz', '--load', 'copy'])
.it('adds an assembly from a compressed FASTA', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -292,11 +283,8 @@ describe('add-assembly', () => {
})
.command(['add-assembly', 'simple.2bit', '--load', 'copy'])
.it('adds an assembly from a 2bit', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -333,11 +321,8 @@ describe('add-assembly', () => {
})
.command(['add-assembly', 'simple.chrom.sizes', '--load', 'copy'])
.it('adds an assembly from a chrom.sizes', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -374,11 +359,8 @@ describe('add-assembly', () => {
})
.command(['add-assembly', 'simple.json', '--load', 'copy'])
.it('adds an assembly from a custom adapter JSON file', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -420,11 +402,8 @@ describe('add-assembly', () => {
'copy',
])
.it('adds an assembly from a custom adapter inline JSON', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -480,11 +459,8 @@ describe('add-assembly', () => {
.it(
"can specify --type when the type can't be inferred from the extension",
async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -541,11 +517,8 @@ describe('add-assembly', () => {
'copy',
])
.it('can specify a custom faiLocation and gziLocation', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -585,14 +558,11 @@ describe('add-assembly', () => {
'copy',
])
.it('can specify a custom name and and alias', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
const contents = await readConf(ctx)
expect(ctx.stdoutWrite).toHaveBeenCalledWith(
'Added assembly "customName" to ./config.json\n',
)
expect(JSON.parse(contents)).toEqual({
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -623,11 +593,8 @@ describe('add-assembly', () => {
'copy',
])
.it('can specify a multiple aliases', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -668,11 +635,8 @@ describe('add-assembly', () => {
'copy',
])
.it('can specify a refNameAliases file', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -723,11 +687,8 @@ describe('add-assembly', () => {
'copy',
])
.it('can specify a custom refNameAliases adapter', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -756,11 +717,8 @@ describe('add-assembly', () => {
'copy',
])
.it('can specify a custom name and and alias', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -795,11 +753,8 @@ describe('add-assembly', () => {
'copy',
])
.it('can use an existing config file', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand All @@ -815,39 +770,20 @@ describe('add-assembly', () => {

setup
.do(async ctx => {
const simple2bit = path.join(
__dirname,
'..',
'..',
'test',
'data',
'simple.2bit',
)
await copyFile(simple2bit, path.join(ctx.dir, path.basename(simple2bit)))
await copyFile(dataDir('simple.2bit'), ctxDir(ctx, 'simple.2bit'))
})
.command(['add-assembly', 'simple.2bit', '--load', 'copy'])
.command(['add-assembly', 'simple.2bit', '--overwrite', '--load', 'copy'])
.it('can use --overwrite to replace an existing assembly', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents).assemblies.length).toBe(1)
const contents = await readConf(ctx)
expect(contents.assemblies.length).toBe(1)
})

setup
.do(async ctx => {
await mkdir('jbrowse')
await rename('manifest.json', path.join('jbrowse', 'manifest.json'))
const simple2bit = path.join(
__dirname,
'..',
'..',
'test',
'data',
'simple.2bit',
)
await copyFile(simple2bit, path.join(ctx.dir, path.basename(simple2bit)))
await copyFile(dataDir('simple.2bit'), path.join(ctx.dir, 'simple.2bit'))
process.chdir('jbrowse')
})
.command([
Expand All @@ -863,11 +799,8 @@ describe('add-assembly', () => {
)
.command(['add-assembly', 'https://mysite.com/data/simple.2bit'])
.it('adds an assembly from a URL', async ctx => {
expect(await readdir(ctx.dir)).toContain('config.json')
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
assemblies: [
{
Expand Down Expand Up @@ -903,10 +836,7 @@ describe('add-assembly', () => {
'testing',
])
.it('can use --out to make a new directory', async ctx => {
const contents = await readFile(
path.join(ctx.dir, 'testing', 'config.json'),
{ encoding: 'utf8' },
)
expect(JSON.parse(contents).assemblies.length).toBe(1)
const contents = await readConf(ctx, 'testing')
expect(contents.assemblies.length).toBe(1)
})
})
22 changes: 8 additions & 14 deletions products/jbrowse-cli/src/commands/add-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path'

import { setup, readConf } from '../testUtil'

const { copyFile, readFile, rename } = fs.promises
const { copyFile, rename } = fs.promises

const defaultConfig = {
assemblies: [
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('add-connection', () => {
.command(['add-connection', 'https://mysite.com/data/hub.txt'])
.it('adds an UCSCTrackHubConnection connection from a url', async ctx => {
const contents = await readConf(ctx)
expect(JSON.parse(contents)).toEqual({
expect(contents).toEqual({
...defaultConfig,
connections: [
{
Expand Down Expand Up @@ -128,10 +128,8 @@ describe('add-connection', () => {
})
.command(['add-connection', 'https://mysite.com/jbrowse/data'])
.it('adds an JBrowse1 connection from a url', async ctx => {
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
connections: [
{
Expand Down Expand Up @@ -194,10 +192,8 @@ describe('add-connection', () => {
'{"url":{"uri":"https://mysite.com/custom"}, "locationType": "UriLocation"}',
])
.it('adds a custom connection with user set fields', async ctx => {
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
connections: [
{
Expand Down Expand Up @@ -272,10 +268,8 @@ describe('add-connection', () => {
.it(
'overwrites an existing custom connection and does not check URL',
async ctx => {
const contents = await readFile(path.join(ctx.dir, 'config.json'), {
encoding: 'utf8',
})
expect(JSON.parse(contents)).toEqual({
const contents = await readConf(ctx)
expect(contents).toEqual({
...defaultConfig,
connections: [
{
Expand Down
Loading

0 comments on commit 42c4632

Please sign in to comment.