<NME> ElmFormatOnFileSaveComponentTest.kt
<BEF> package org.elm.ide.components

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.undo.UndoManager
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import junit.framework.TestCase
import org.elm.workspace.ElmWorkspaceTestBase
import org.elm.workspace.elmWorkspace
import org.intellij.lang.annotations.Language

class ElmFormatOnFileSaveComponentTest : ElmWorkspaceTestBase() {

/*
    override fun runTest() {
        if (toolchain.elmFormatCLI == null) {
            // TODO in the future maybe we should install elm-format in the CI build environment
            System.err.println("SKIP $name: elm-format not found")
            return
        }
        super.runTest()
    }
*/

    val unformatted = """
                    module Main exposing (f)


                    f x = x

                """.trimIndent()

    val expectedFormatted = """
                    module Main exposing (f)


                    f x =
                        x

                """.trimIndent()

    fun `test ElmFormatOnFileSaveComponent should work with elm 19`() {

        buildProject {
            project("elm.json", manifestElm19)
            dir("src") {
                elm("Main.elm", unformatted)
            }
        }

        testCorrectFormatting("src/Main.elm", unformatted, expectedFormatted)
    }

    fun `test ElmFormatOnFileSaveComponent should not add to the undo stack`() {

        buildProject {
            project("elm.json", manifestElm19)
            dir("src") {
                elm("Main.elm", unformatted)
            }
        }

        val inputPath = "src/Main.elm"
        testCorrectFormatting(inputPath, unformatted, expectedFormatted)
        val file = myFixture.configureFromTempProjectFile(inputPath).virtualFile
        val fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(file)

        val undoManager = UndoManager.getInstance(project)
        TestCase.assertFalse(undoManager.isUndoAvailable(fileEditor))
    }

    fun `test ElmFormatOnFileSaveComponent should not touch a file with the wrong ending like 'scala'`() {
        buildProject {
            project("elm.json", manifestElm19)
            dir("src") {
                elm("Main.elm")
                file("Main.scala", "blah")
            }
        }

        testCorrectFormatting("src/Main.scala", unformatted, expected = unformatted)
    }

    fun `test ElmFormatOnFileSaveComponent should not touch a file if the save-hook is deactivated`() {
        buildProject {
            project("elm.json", manifestElm19)
            dir("src") {
                elm("Main.elm", """
                    module Main exposing (f)


                    f x = x

                """.trimIndent())
            }
        }

        testCorrectFormatting(
                "src/Main.elm",
                unformatted,
                expected = unformatted,
                activateOnSaveHook = false
        )
    }

    private fun testCorrectFormatting(fileWithCaret: String, unformatted: String, expected: String, activateOnSaveHook: Boolean = true) {

        )
    }

    private fun testCorrectFormatting(fileWithCaret: String, unformatted: String, expected: String, activateOnSaveHook: Boolean = true) {

        project.elmWorkspace.useToolchain(toolchain?.copy(isElmFormatOnSaveEnabled = activateOnSaveHook))
                "direct": {
                    "elm/core": "1.0.0",
                    "elm/json": "1.0.0"
                },
                "indirect": {}
            },
            "test-dependencies": {
                "direct": {},
                "indirect": {}
            }
        }
        """.trimIndent()

        TestCase.assertEquals(expected, document.text)
    }

}



<MSG> elm-format action and on-file-save disabled on files with errors (#283)

If the current file has syntax errors and...
- the user explicitly invoked elm-format: an error will be shown
- elm-format was invoked automatically via the save hook: no error will be shown
<DFF> @@ -108,6 +108,28 @@ class ElmFormatOnFileSaveComponentTest : ElmWorkspaceTestBase() {
         )
     }
 
+    fun `test ElmFormatOnFileSaveComponent should not touch a file syntax errors`() {
+        val brokenElmCode = """
+                    m0dule Main exposing (f)
+
+
+                    f x = x{-caret-}
+
+                """.trimIndent()
+        val fileWithCaret = buildProject {
+            project("elm.json", manifestElm19)
+            dir("src") {
+                elm("Main.scala", brokenElmCode)
+            }
+        }.fileWithCaret
+
+        testCorrectFormatting(
+                fileWithCaret,
+                brokenElmCode,
+                expected = brokenElmCode.replace("{-caret-}", "")
+        )
+    }
+
     private fun testCorrectFormatting(fileWithCaret: String, unformatted: String, expected: String, activateOnSaveHook: Boolean = true) {
 
         project.elmWorkspace.useToolchain(toolchain?.copy(isElmFormatOnSaveEnabled = activateOnSaveHook))
@@ -127,7 +149,6 @@ class ElmFormatOnFileSaveComponentTest : ElmWorkspaceTestBase() {
 
         TestCase.assertEquals(expected, document.text)
     }
-
 }
 
 

elm-format action and on-file-save disabled on files with errors (#283)