# all files / cli/init/ create-test-directory-structure.js

100% Statements 29/29
100% Branches 9/9
100% Functions 8/8
100% Lines 14/14
1 branch Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56

    1

    1
    4
    4

    1
    4

    28

    4

    1
    28

    14
    7

    7

     'use strict';

    // Constants:
    import constants from '../../constants';

    // Utilities:
    import Promise from 'bluebird';
    const fs = Promise.promisifyAll(require('graceful-fs'));
    import log from 'npmlog';
    import { join } from 'path';

    // Errors:
    import TractorError from '../../errors/TractorError';

    export default {
        run: createTestDirectoryStructure
    };

    function createTestDirectoryStructure (testDirectory) {
        log.info('Creating directory structure...');
        return createAllDirectories(testDirectory);
    }

    function createAllDirectories (testDirectory) {
        let createDirectories = [
            /* eslint-disable no-warning-comments */
            // TODO: This is a bit cryptic, pull this out into another promise
            // that creates the root dir, and do that first. Otherwise there
            // may be a race condition here?
            '',
            constants.COMPONENTS,
            constants.FEATURES,
            constants.STEP_DEFINITIONS,
            constants.MOCK_DATA,
            constants.SUPPORT_DIR,
            constants.REPORT_DIR
        ].map((directory) => {
            return createDir(join(testDirectory, directory))
                .catch(TractorError, (error) => log.warn(`${error.message} Moving on...`));
        });

        return Promise.all(createDirectories)
        .then(() => log.verbose('Directory structure created.'));
    }

    function createDir (dir) {
        return fs.mkdirAsync(dir)
        .catch(Promise.OperationalError, (error) => {
            if (error && error.cause && error.cause.code === 'EEXIST') {
                throw new TractorError(`"${dir}" directory already exists.`);
            } else {
                throw error;
            }
        });
    }