Skip to content

Commit

Permalink
Chore: Make cleaning stuff
Browse files Browse the repository at this point in the history
A config folder
  • Loading branch information
jy95 committed Dec 23, 2017
1 parent 5ec79c5 commit 6ca2560
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 93 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -13,8 +13,8 @@
"compile": "babel -d lib/ src/ --source-maps both",
"prepare": "npm run compile",
"test": "mocha",
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose",
"lint": "./node_modules/.bin/eslint ./src ./test --fix"
"generate-docs": "node_modules/.bin/jsdoc --configure config/.jsdoc.json --verbose",
"lint": "./node_modules/.bin/eslint -c config/.eslintrc.js ./src ./test --fix"
},
"engines": {
"node": ">=6",
Expand Down Expand Up @@ -58,7 +58,7 @@
"eslint-config-airbnb-base": "^12.0.2",
"eslint-plugin-import": "^2.7.0",
"istanbul": "^1.1.0-alpha.1",
"jsdoc": "^3.5.4",
"jsdoc": "^3.5.5",
"minami": "^1.2.3",
"mocha": "^4.0.0",
"semantic-release": "^11.0.2",
Expand Down
50 changes: 20 additions & 30 deletions src/TorrentLibrary.js
Expand Up @@ -120,16 +120,15 @@ class TorrentLibrary extends EventEmitter {
* @param {(Set.<TorrentLibrary~TPN_Extended>)} [config.movies=new Set()] - the movies files
* @param {(Map.<string, Set.<TorrentLibrary~TPN_Extended>>)} [config.series=new Map()] - the serie files
*/
constructor(
{
defaultPath = process.cwd()
/* istanbul ignore next: tired of writing tests */,
paths = [] /* istanbul ignore next: tired of writing tests */,
allFilesWithCategory = new Map()
/* istanbul ignore next: tired of writing tests */,
movies = new Set() /* istanbul ignore next: tired of writing tests */,
series = new Map() /* istanbul ignore next: tired of writing tests */,
} = {} /* istanbul ignore next: tired of writing tests */) {
constructor({
defaultPath = process.cwd()
/* istanbul ignore next: tired of writing tests */,
paths = [] /* istanbul ignore next: tired of writing tests */,
allFilesWithCategory = new Map()
/* istanbul ignore next: tired of writing tests */,
movies = new Set() /* istanbul ignore next: tired of writing tests */,
series = new Map() /* istanbul ignore next: tired of writing tests */,
} = {} /* istanbul ignore next: tired of writing tests */) {
super();
/**
* just an easy way to scan the current directory path, if not other paths provided
Expand Down Expand Up @@ -213,18 +212,14 @@ class TorrentLibrary extends EventEmitter {
// add the tv series into newTvSeries
// First step : find all the series not in newTvSeries and add them to newTvSeries
difference(
uniq(
[...tvSeriesSet].map(tvSeries => tvSeries.title),
),
uniq([...tvSeriesSet].map(tvSeries => tvSeries.title)),
...newTvSeries.keys(),
).forEach((tvSeriesToInsert) => {
newTvSeries.set(tvSeriesToInsert, new Set());
});

// Second step : add the new files into the correct tvSeries Set
uniq(
[...tvSeriesSet].map(tvSeries => tvSeries.title),
).forEach((tvSerie) => {
uniq([...tvSeriesSet].map(tvSeries => tvSeries.title)).forEach((tvSerie) => {
// get the current set for this tvSerie
const currentTvSerie = newTvSeries.get(tvSerie);

Expand Down Expand Up @@ -380,30 +375,25 @@ class TorrentLibrary extends EventEmitter {
that.categoryForFile.get(file) === TorrentLibrary.TV_SERIES_TYPE);

// for movies, just an easy removal
that.stores.set(TorrentLibrary.MOVIES_TYPE,
new Set(
[...that.allMovies]
.filter(movie => !(processData[1].includes(movie.filePath))),
),
that.stores.set(
TorrentLibrary.MOVIES_TYPE,
new Set([...that.allMovies]
.filter(movie => !(processData[1].includes(movie.filePath)))),
);

// for the tv-series, a bit more complicated
// first step : find the unique tv series of these files
const tvSeriesShows = uniq(
processData[0]
.map(file => nameParser(basename(file)).title),
);
const tvSeriesShows = uniq(processData[0]
.map(file => nameParser(basename(file)).title));

// second step : foreach each series in tvSeriesShows
const newTvSeriesMap = that.allTvSeries;

for (const serie of tvSeriesShows) {
// get the set for this serie
const filteredSet = new Set(
[...newTvSeriesMap.get(serie)]
.filter(episode =>
!(processData[0].includes(episode.filePath))),
);
const filteredSet = new Set([...newTvSeriesMap.get(serie)]
.filter(episode =>
!(processData[0].includes(episode.filePath))));
// if the filtered set is empty => no more episodes for this series
if (filteredSet.size === 0) {
newTvSeriesMap.delete(serie);
Expand Down
158 changes: 105 additions & 53 deletions test/APITests.js
@@ -1,3 +1,4 @@
/* eslint-disable prefer-destructuring */
import videosExtension from 'video-extensions';
import assert from 'assert';
import path from 'path';
Expand All @@ -13,17 +14,23 @@ describe('TorrentLibrary tests', () => {
const folders = [path.join(__dirname, 'folder1'),
path.join(__dirname, 'folder2')];
const files = [
path.join(__dirname, 'folder1',
'The.Blacklist.S04E21.FRENCH.WEBRip.XviD.avi'),
path.join(__dirname, 'folder2',
'The.Blacklist.S04E14.FRENCH.WEBRip.XviD.avi'),
path.join(__dirname, 'folder1',
path.join(
__dirname, 'folder1',
'The.Blacklist.S04E21.FRENCH.WEBRip.XviD.avi',
),
path.join(
__dirname, 'folder2',
'The.Blacklist.S04E14.FRENCH.WEBRip.XviD.avi',
),
path.join(
__dirname, 'folder1',
'Bad.Ass.2012.LiMiTED.TRUEFRENCH.DVDRiP.XviD' +
'-www.zone-telechargement.ws.avi'),
'-www.zone-telechargement.ws.avi',
),
];
let expectedJson = JSON.parse(
fs.readFileSync(path.join(__dirname, 'example.json'), 'utf8'),
);

// eslint-disable-next-line max-len
let expectedJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'example.json'), 'utf8'));

// initialization
before(() => {
Expand All @@ -43,21 +50,27 @@ describe('TorrentLibrary tests', () => {
describe('Static methods', () => {
context('listVideosExtension()', () => {
it('should provide the same list of videos extensions', () => {
assert.deepEqual(JSON.stringify(videosExtension),
assert.deepEqual(
JSON.stringify(videosExtension),
JSON.stringify(TorrentLibrary.listVideosExtension()),
'Not the same JSON');
'Not the same JSON',
);
});
});

context('Constants', () => {
it('MOVIES_TYPE', () => {
assert.equal(TorrentLibrary.MOVIES_TYPE,
'MOVIES', 'Someone changed this constant value !');
assert.equal(
TorrentLibrary.MOVIES_TYPE,
'MOVIES', 'Someone changed this constant value !',
);
});

it('TV_SERIES_TYPE', () => {
assert.equal(TorrentLibrary.TV_SERIES_TYPE,
'TV_SERIES', 'Someone changed this constant value !');
assert.equal(
TorrentLibrary.TV_SERIES_TYPE,
'TV_SERIES', 'Someone changed this constant value !',
);
});
});
});
Expand All @@ -74,8 +87,10 @@ describe('TorrentLibrary tests', () => {
.catch(() => {
assert(eventSpy.called, 'Event did not fire.');
assert(eventSpy.calledOnce, 'Event fired more than once');
assert.equal(libInstance.hasPathsProvidedByUser(), false,
'No paths by user should be added');
assert.equal(
libInstance.hasPathsProvidedByUser(), false,
'No paths by user should be added',
);
done();
});
});
Expand All @@ -90,8 +105,10 @@ describe('TorrentLibrary tests', () => {
.catch(() => {
assert(eventSpy.called, 'Event did not fire.');
assert(eventSpy.calledOnce, 'Event fired more than once');
assert.equal(libInstance.hasPathsProvidedByUser(), false,
'No paths by user should be added');
assert.equal(
libInstance.hasPathsProvidedByUser(), false,
'No paths by user should be added',
);
done();
});
});
Expand All @@ -104,8 +121,10 @@ describe('TorrentLibrary tests', () => {
.then(() => {
assert(eventSpy.called, 'Event did not fire.');
assert(eventSpy.calledOnce, 'Event fired more than once');
assert.equal(libInstance.hasPathsProvidedByUser(), true,
'The path should be added');
assert.equal(
libInstance.hasPathsProvidedByUser(), true,
'The path should be added',
);
done();
}).catch((err) => {
done(err);
Expand Down Expand Up @@ -199,29 +218,40 @@ describe('TorrentLibrary tests', () => {

context('createFromJSON()', () => {
it('It should create a perfect copy of instance', () => {
const createdInstance = TorrentLibrary.createFromJSON(
JSON.parse(libInstance.toJSON()),
);
assert.equal(_.isEqual(createdInstance.allFilesWithCategory,
createdInstance.allFilesWithCategory), true);
assert.equal(_.isEqual(createdInstance.allMovies,
libInstance.allMovies), true);
assert.equal(_.isEqual(createdInstance.allTvSeries,
libInstance.allTvSeries), true);
const jsonFromLib = JSON.parse(libInstance.toJSON());
const createdInstance = TorrentLibrary.createFromJSON(jsonFromLib);
assert.equal(_.isEqual(
createdInstance.allFilesWithCategory,
createdInstance.allFilesWithCategory,
), true);
assert.equal(_.isEqual(
createdInstance.allMovies,
libInstance.allMovies,
), true);
assert.equal(_.isEqual(
createdInstance.allTvSeries,
libInstance.allTvSeries,
), true);
});
});

context('Remove Old files', () => {
it('Should not be able to remove not present files', () => {
const wrongFile = path.join(__dirname, 'folder1',
'The.Blacklist.S04E22.FRENCH.WEBRip.XviD.avi');
const wrongFile = path.join(
__dirname, 'folder1',
'The.Blacklist.S04E22.FRENCH.WEBRip.XviD.avi',
);
const allFiles = libInstance.allFilesWithCategory;
const expectedTvSeriesMap = libInstance.allTvSeries;
libInstance.removeOldFiles(wrongFile);
assert.equal(_.isEqual(allFiles, libInstance.allFilesWithCategory),
true, 'nothing should have changed!');
assert.equal(_.isEqual(expectedTvSeriesMap,
libInstance.allTvSeries), true, 'nothing should have changed!');
assert.equal(
_.isEqual(allFiles, libInstance.allFilesWithCategory),
true, 'nothing should have changed!',
);
assert.equal(_.isEqual(
expectedTvSeriesMap,
libInstance.allTvSeries,
), true, 'nothing should have changed!');
});

it('Should be able to remove a movie', () => {
Expand All @@ -232,11 +262,17 @@ describe('TorrentLibrary tests', () => {
let eventSpy = sinon.spy();
libInstance.on('removeOldFiles', eventSpy);
libInstance.removeOldFiles(files[2]);
assert.equal(_.isEqual(allFilesWithoutMovie,
libInstance.allFilesWithCategory), true,
'The movie should have been removed!');
assert.equal(_.isEqual(expectedMovieSet,
libInstance.allMovies), true, 'The movie should have been removed!');
assert.equal(
_.isEqual(
allFilesWithoutMovie,
libInstance.allFilesWithCategory,
), true,
'The movie should have been removed!',
);
assert.equal(_.isEqual(
expectedMovieSet,
libInstance.allMovies,
), true, 'The movie should have been removed!');
assert(eventSpy.called, 'Event did not fire.');
assert(eventSpy.calledOnce, 'Event fired more than once');
});
Expand All @@ -256,13 +292,21 @@ describe('TorrentLibrary tests', () => {
]);
tempInstance.removeOldFiles(files[1]);

assert.equal(_.isEqual(allFiles,
tempInstance.allFilesWithCategory), true,
'The tv-series episode should have been removed!');
assert.equal(
_.isEqual(
allFiles,
tempInstance.allFilesWithCategory,
), true,
'The tv-series episode should have been removed!',
);
// Fix ici
assert.equal(_.isEqual(expectedSeriesMap,
tempInstance.allTvSeries), true,
'The tv-series should still exist');
assert.equal(
_.isEqual(
expectedSeriesMap,
tempInstance.allTvSeries,
), true,
'The tv-series should still exist',
);
assert(eventSpy.called, 'Event did not fire.');
assert(eventSpy.calledOnce, 'Event fired more than once');
});
Expand All @@ -273,12 +317,20 @@ describe('TorrentLibrary tests', () => {
let eventSpy = sinon.spy();
libInstance.on('removeOldFiles', eventSpy);
libInstance.removeOldFiles(...files.slice(0, 2));
assert.equal(_.isEqual(allFiles,
libInstance.allFilesWithCategory), true,
'The tv-series episodes should have all been removed!');
assert.equal(_.isEqual(expectedSeriesMap,
libInstance.allTvSeries), true,
'The tv-series episodes should have all been removed!');
assert.equal(
_.isEqual(
allFiles,
libInstance.allFilesWithCategory,
), true,
'The tv-series episodes should have all been removed!',
);
assert.equal(
_.isEqual(
expectedSeriesMap,
libInstance.allTvSeries,
), true,
'The tv-series episodes should have all been removed!',
);
assert(eventSpy.called, 'Event did not fire.');
assert(eventSpy.calledOnce, 'Event fired more than once');
});
Expand Down

0 comments on commit 6ca2560

Please sign in to comment.