'use strict';

var equal = require('deep-equal'),
    assert = require('assert'),
    yaml = require('js-yaml'),
    fs = require('fs'),
    path = require('path'),
    merge = require('merge'),
    config = require('../lib/config');

var custOptions = function () {
  return {
    'options': {
      'formatter': 'stylish',
      'cache-config': false
    },
    'files': {
      'ignore': [
        'foo',
        'bar'
      ]
    },
    'rules': {
      'no-duplicate-property': 0,
      'indentation': [
        2,
          {
            'size': 4
          }
      ]
    }
  };
};

describe('config', function () {
  it('should return the defaults if no config is passed in', function (done) {
    var defaultConfig = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', 'lib', 'config', 'sass-lint.yml'), 'utf8')),
        conf = config();

    assert(
      equal(
        conf,
        defaultConfig,
        {
          'strict': true
        }
      )
    );

    done();
  });

  it('should merge options when passed in', function (done) {

    var defaultConfig = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', 'lib', 'config', 'sass-lint.yml'), 'utf8')),
        tempOptions = custOptions(),
        conf = config(tempOptions),
        merged = merge.recursive(defaultConfig, tempOptions);

    assert(
      equal(
        conf,
        merged,
        {
          'strict': true
        }
      )
    );

    done();
  });

  it('should use a config file if one is provided', function (done) {
    var defaultConfig = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', 'lib', 'config', 'sass-lint.yml'), 'utf8')),
        tempOptions = {
          'options': {
            'config-file': 'tests/yml/.stylish-output.yml',
            'cache-config': false
          }
        },
        conf = config(tempOptions),
        merged = merge.recursive(tempOptions, defaultConfig, yaml.safeLoad(fs.readFileSync(path.join(__dirname, 'yml', '.stylish-output.yml'), 'utf8')));

    assert(
      equal(
        conf,
        merged,
        {
          'strict': true
        }
      )
    );

    done();
  });

  it('should not merge rules when `merge-default-rules` is false', function (done) {
    var defaultConfig = yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', 'lib', 'config', 'sass-lint.yml'), 'utf8')),
        tempOptions = custOptions(),
        conf,
        merged;

    tempOptions.options['merge-default-rules'] = false;

    conf = config(tempOptions);

    merged = merge.recursive(tempOptions, defaultConfig);
    merged.rules = tempOptions.rules;

    assert(
      equal(
        conf,
        merged,
        {
          'strict': true
        }
      )
    );

    done();
  });
});

@@ -2469,17 +2469,16 @@
  false;%0A
-%0A
     conf
@@ -2497,25 +2497,24 @@
 mpOptions);%0A
-%0A
     merged =
@@ -2629,32 +2629,32 @@
 (%0A        conf,%0A
-
         merged,%0A
@@ -2706,32 +2706,681 @@
       )%0A    );%0A%0A
+    done();%0A  %7D);%0A%0A  it('should load a cached version of the config', function (done) %7B%0A    var tempOptions = %7B%0A          'options': %7B%7D%0A        %7D,%0A        confFirst,%0A        confSecond;%0A%0A    tempOptions.options%5B'cache-config'%5D = true;%0A%0A    // first pass at config to set the cache%0A    confFirst = config(tempOptions);%0A%0A    // second pass to check if cache loads adding in a config file that shouldn't be loaded%0A    tempOptions.options%5B'config-file'%5D = 'tests/yml/.stylish-output.yml';%0A    confSecond = config(tempOptions);%0A%0A    assert(%0A      equal(%0A        confFirst,%0A        confSecond,%0A        %7B%0A          'strict': true%0A        %7D%0A      )%0A    );%0A%0A
     done();%0A  %7D)

Add test for cached config