fix(core): custom album mappers using file:// can use a relative path

pull/107/head
Romain 6 years ago
parent 253a0b5fff
commit 8bbe1186c5

@ -63,7 +63,8 @@
"mock-fs": "^4.5.0",
"require-lint": "^1.3.0",
"should": "^13.2.1",
"standard": "^11.0.1"
"standard": "^11.0.1",
"tmp": "0.0.33"
},
"standard": {
"ignore": [

@ -6,6 +6,7 @@ based on the --albums-from array of patterns provided
*/
const _ = require('lodash')
const path = require('path')
const albumPattern = require('./album-pattern')
class AlbumMapper {
@ -22,7 +23,7 @@ function load (pattern) {
// custom mapper file
if (typeof pattern === 'string' && pattern.startsWith('file://')) {
const filepath = pattern.slice('file://'.length)
return require(filepath)
return require(path.resolve(filepath))
}
// string pattern
if (typeof pattern === 'string') {

@ -1,4 +1,8 @@
const fs = require('fs')
const path = require('path')
const os = require('os')
const should = require('should/as-function')
const tmp = require('tmp')
const AlbumMapper = require('../../src/input/album-mapper.js')
const fixtures = require('../fixtures.js')
@ -12,23 +16,62 @@ describe('Album mapper', function () {
const mapper = new AlbumMapper(['%path'])
should(mapper.getAlbums(TEST_FILE)).eql(['Holidays'])
})
it('can use a single function (for testing)', function () {
const custom = file => 'hello'
const mapper = new AlbumMapper([custom])
should(mapper.getAlbums(TEST_FILE)).eql(['hello'])
})
it('can provide multiple string patterns', function () {
const mapper = new AlbumMapper(['%path', '{YYYY}'])
should(mapper.getAlbums(TEST_FILE)).eql(['Holidays', '2016'])
})
it('merges all albums into a single array', function () {
const custom1 = file => ['one']
const custom2 = file => ['two', 'three']
const mapper = new AlbumMapper([custom1, custom2])
should(mapper.getAlbums(TEST_FILE)).eql(['one', 'two', 'three'])
})
it('defaults to %path if no patterns are specified', () => {
const mapper = new AlbumMapper()
should(mapper.getAlbums(TEST_FILE)).eql(['Holidays'])
})
describe('custom function using file://', () => {
it('with an absolute path', () => {
const absolutePath = createTmpFile({
dir: os.tmpdir(),
contents: "module.exports = file => ['my-album']"
})
const mapper = new AlbumMapper([`file://${absolutePath}`])
should(mapper.getAlbums(TEST_FILE)).eql(['my-album'])
})
it('with a path relative to the current directory', () => {
const absolutePath = createTmpFile({
dir: `${process.cwd()}/tmp`,
contents: "module.exports = file => ['my-album']"
})
const relative = path.relative(process.cwd(), absolutePath)
const mapper = new AlbumMapper([`file://${relative}`])
should(mapper.getAlbums(TEST_FILE)).eql(['my-album'])
})
})
})
/*
Create a temporary file.
It will automatically be deleted when the tests finish.
*/
function createTmpFile (opts) {
const file = tmp.fileSync({
dir: opts.dir,
discardDescriptor: true
})
fs.writeFileSync(file.name, opts.contents)
// return the absolute path to the file
return file.name
}

Loading…
Cancel
Save