"use strict"

const os = require("os")
const path = require("path")
const fs = require("fs")
const _ = require("lodash")
const pify = require("pify")
const del = require("del")
const cpFile = require("cp-file")
const systemTestUtils = require("../systemTestUtils")
const stylelint = require("../../lib")
const spawn = require("child_process").spawn

describe("fix", () => {
  let tmpDir
  let stylesheetPath

  beforeEach(() => {
    tmpDir = os.tmpdir()
    stylesheetPath = path.join(tmpDir, `stylesheet-${_.uniqueId()}.css`)
    return cpFile(path.join(__dirname, "stylesheet.css"), stylesheetPath)
  })

  afterEach(() => {
    return del(stylesheetPath, { force: true })
  })

  it("expected stylelint results", () => {
    return stylelint.lint({
      files: [stylesheetPath],
      configFile: systemTestUtils.caseConfig("fix"),
      fix: true,
    }).then((output) => {
      // Remove the path to tmpDir
      const cleanedResults = output.results.map(r => Object.assign({}, r, { source: "stylesheet.css" }))
      expect(systemTestUtils.prepResults(cleanedResults)).toMatchSnapshot()
    })
  })

  it("expected fixes to PostCSS Result", () => {
    return stylelint.lint({
      files: [stylesheetPath],
      configFile: systemTestUtils.caseConfig("fix"),
      fix: true,
    }).then((output) => {
      const result = output.results[0]._postcssResult
      expect(result.root.toString(result.opts.syntax)).toMatchSnapshot()
    })
  })

  it("overwrites the original file", () => {
    return stylelint.lint({
      files: [stylesheetPath],
      configFile: systemTestUtils.caseConfig("fix"),
      fix: true,
    }).then((output) => {
      const result = output.results[0]._postcssResult
      return pify(fs.readFile)(stylesheetPath, "utf8").then((fileContent) => {
        expect(fileContent).toBe(result.root.toString(result.opts.syntax))
      })
    })
  })

  it("should not fix ignored files", () => {
    const readFile = () => {
      return pify(fs.readFile)(stylesheetPath, "utf8")
    }

    const runCli = () => {
      return new Promise((r, rj) => {
        const localPath = path.resolve(__dirname)
        const cliPath = path.join(localPath, "../../lib/cli.js")

        const cliProcess = spawn("node", [
          cliPath, stylesheetPath,
          "--ignore-path", "custom-ignore",
          "--config", "config.json",
          "--fix",
        ], { cwd: localPath })

        cliProcess.on("error", function (error) {
          rj(error)
        })

        cliProcess.on("close", function (code) {
          r(code)
        })
      })
    }

    return (readFile()
      .then((originalContent) => {
        return (runCli()
          .then((code) => {
            expect(code).toEqual(0)
          })
          .then(readFile)
          .then((newContent) => {
            expect(newContent).toBe(originalContent)
          })
        )
      })
    )
  })
})

@@ -300,53 +300,8 @@
 ib%22)
-%0Aconst spawn = require(%22child_process%22).spawn
 %0A%0Ade
@@ -1841,22 +1841,24 @@
 it(%22
-should not fix
+doesn't write to
  ign
@@ -1866,17 +1866,16 @@
 red file
-s
 %22, () =%3E
@@ -1885,555 +1885,257 @@
     
-const readFile = () =%3E %7B%0A      return pify(fs.readFile)(stylesheetPath, %22utf8%22)%0A    %7D%0A%0A    const runCli = () =%3E %7B%0A      return new Promise((r, rj) =%3E %7B%0A        const localPath = path.resolve(__dirname)%0A        const cliPath = path.join(localPath, %22../../lib/cli.js%22)%0A%0A        const cliProcess = spawn(%22node%22, %5B%0A          cliPath, stylesheetPath,%0A          %22--ignore-path%22, %22custom-ignore%22,%0A          %22--config%22, %22config.json%22,%0A          %22--fix%22,%0A        %5D, %7B cwd: localPath %7D)%0A%0A        cliProcess.on(%22error%22, function (error) %7B%0A          rj(error)
+return stylelint.lint(%7B%0A      files: %5BstylesheetPath%5D,%0A      config: %7B%0A        ignoreFiles: stylesheetPath,%0A        rules: %7B%0A          %22at-rule-name-case%22: %22lower%22,%0A          %22comment-empty-line-before%22: %22always%22,%0A          %22comment-no-empty%22: true,
 %0A   
@@ -2144,327 +2144,232 @@
    %7D
-)%0A
+,
 %0A      
-  cliProcess.on(%22close%22, function (code) %7B%0A          r(code)%0A        %7D)%0A      %7D)%0A    %7D%0A%0A    return (readFile()%0A      .then((originalContent) =%3E %7B%0A        return (runCli()%0A          .then((code) =%3E %7B%0A            expect(code).toEqual(0)%0A          %7D)%0A          
+%7D,%0A      fix: true,%0A    %7D).then(() =%3E %7B%0A      return pify(fs.readFile)(stylesheetPath, %22utf8%22).then((newFile) =%3E %7B%0A        return pify(fs.readFile)(path.join(__dirname, %22stylesheet.css%22), %22utf8%22)
 .then(
-rea
+(ol
 dFile)
-%0A          .then((newContent)
  =%3E %7B%0A
-  
     
@@ -2388,23 +2388,20 @@
 (new
-Content
+File
 ).toBe(o
 rigi
@@ -2400,45 +2400,25 @@
 Be(o
-riginalContent)%0A          %7D)%0A        
+ldFile)%0A        %7D
 )%0A  
@@ -2428,16 +2428,17 @@
  %7D)%0A    
+%7D
 )%0A  %7D)%0A%7D

Use stylelint.lint test