2018-06-08 21:28:11 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2017-12-23 09:32:10 +00:00
|
|
|
const should = require('should/as-function')
|
2018-06-08 21:28:11 +00:00
|
|
|
const tmp = require('tmp')
|
2017-12-23 09:32:10 +00:00
|
|
|
const AlbumMapper = require('../../src/input/album-mapper.js')
|
|
|
|
const fixtures = require('../fixtures.js')
|
|
|
|
|
|
|
|
const TEST_FILE = fixtures.photo({
|
|
|
|
path: 'Holidays/IMG_0001.jpg',
|
|
|
|
date: '2016:07:14 12:07:41'
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Album mapper', function () {
|
|
|
|
it('can use a single string pattern', function () {
|
2017-12-22 21:37:08 +00:00
|
|
|
const mapper = new AlbumMapper(['%path'])
|
2017-12-23 09:32:10 +00:00
|
|
|
should(mapper.getAlbums(TEST_FILE)).eql(['Holidays'])
|
|
|
|
})
|
2018-06-08 21:28:11 +00:00
|
|
|
|
2017-12-23 09:32:10 +00:00
|
|
|
it('can use a single function (for testing)', function () {
|
|
|
|
const custom = file => 'hello'
|
2017-12-22 21:37:08 +00:00
|
|
|
const mapper = new AlbumMapper([custom])
|
2017-12-23 09:32:10 +00:00
|
|
|
should(mapper.getAlbums(TEST_FILE)).eql(['hello'])
|
|
|
|
})
|
2018-06-08 21:28:11 +00:00
|
|
|
|
2017-12-23 09:32:10 +00:00
|
|
|
it('can provide multiple string patterns', function () {
|
2017-12-22 21:37:08 +00:00
|
|
|
const mapper = new AlbumMapper(['%path', '{YYYY}'])
|
2017-12-23 09:32:10 +00:00
|
|
|
should(mapper.getAlbums(TEST_FILE)).eql(['Holidays', '2016'])
|
|
|
|
})
|
2018-06-08 21:28:11 +00:00
|
|
|
|
2017-12-23 09:32:10 +00:00
|
|
|
it('merges all albums into a single array', function () {
|
|
|
|
const custom1 = file => ['one']
|
|
|
|
const custom2 = file => ['two', 'three']
|
2017-12-22 21:37:08 +00:00
|
|
|
const mapper = new AlbumMapper([custom1, custom2])
|
2017-12-23 09:32:10 +00:00
|
|
|
should(mapper.getAlbums(TEST_FILE)).eql(['one', 'two', 'three'])
|
|
|
|
})
|
2018-06-08 21:28:11 +00:00
|
|
|
|
2017-12-22 21:37:08 +00:00
|
|
|
it('defaults to %path if no patterns are specified', () => {
|
|
|
|
const mapper = new AlbumMapper()
|
|
|
|
should(mapper.getAlbums(TEST_FILE)).eql(['Holidays'])
|
2017-12-23 09:32:10 +00:00
|
|
|
})
|
2018-06-08 21:28:11 +00:00
|
|
|
|
|
|
|
describe('custom function using file://', () => {
|
|
|
|
it('with an absolute path', () => {
|
|
|
|
const absolutePath = createTmpFile({
|
|
|
|
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({
|
2020-07-15 22:37:03 +00:00
|
|
|
tmpdir: process.cwd(),
|
2018-06-08 21:28:11 +00:00
|
|
|
contents: "module.exports = file => ['my-album']"
|
|
|
|
})
|
2018-06-09 21:46:21 +00:00
|
|
|
const relative = path.basename(absolutePath)
|
2018-06-08 21:28:11 +00:00
|
|
|
const mapper = new AlbumMapper([`file://${relative}`])
|
|
|
|
should(mapper.getAlbums(TEST_FILE)).eql(['my-album'])
|
|
|
|
})
|
|
|
|
})
|
2017-12-23 09:32:10 +00:00
|
|
|
})
|
2018-06-08 21:28:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Create a temporary file.
|
|
|
|
It will automatically be deleted when the tests finish.
|
|
|
|
*/
|
|
|
|
function createTmpFile (opts) {
|
|
|
|
const file = tmp.fileSync({
|
2020-07-15 22:37:03 +00:00
|
|
|
tmpdir: opts.tmpdir || undefined,
|
2018-06-08 21:28:11 +00:00
|
|
|
discardDescriptor: true
|
|
|
|
})
|
|
|
|
fs.writeFileSync(file.name, opts.contents)
|
|
|
|
// return the absolute path to the file
|
|
|
|
return file.name
|
|
|
|
}
|