Repository: JetBrains/kotlin
File: compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt (Kotlin)

Code before:
            expectedExitCode = 1
        )
    }

    fun testRunClassFileWithExtension() {
        runProcess("kotlinc", "$testDataDirectory/helloWorld.kt", "-d", tmpdir.path)
        runProcess(
            "kotlin",
            "test/HelloWorldKt.class",
            expectedStdout = "Hello!\n",
            workDirectory = tmpdir
        )
        runProcess(
            "kotlin",
            "test.HelloWorldKt.class",
            expectedExitCode = 1,
            expectedStderr = "error: could not find or load main class test.HelloWorldKt.class\n",
            workDirectory = tmpdir
        )

        val subDir2 = File("$tmpdir/subDir1/subDir2").also { it.mkdirs() }
        val subDir3 = File("$tmpdir/subDir3").also { it.mkdir() }
        val subDir1 = subDir2.parentFile
        fun makeSureTheClassFileExists(classFile: File) {
            if (!classFile.exists()) {
                throw FileNotFoundException("class file $classFile is not found")
            }
        }
        runProcess("kotlinc", "$testDataDirectory/defaultPackage.kt", "-d", subDir1.path)
        runProcess(
            "kotlin",
            "DefaultPackageKt.class",
            expectedStdout = "in default package\n",
            workDirectory = subDir1
        )
        runProcess(
            "kotlin",
            "./DefaultPackageKt.class",
            expectedStdout = "in default package\n",
            workDirectory = subDir1
        )
        runProcess(
            "kotlin",
            "./subDir2/../DefaultPackageKt.class",
            expectedStdout = "in default package\n",
            workDirectory = subDir1
        )
        runProcess(
            "kotlin",
            "subDir1/DefaultPackageKt.class",
            expectedExitCode = 1,

Review comment (bug):
Minor, but let's throw `AssertionError` to make it clear that the test fails because of incorrect compiler behavior, not some I/O problems

Code after:

        runProcess("kotlinc", "$testDataDirectory/defaultPackage.kt", "-d", testDir.path)
        assertExists(File("${testDir.path}/DefaultPackageKt.class"))

        runProcess(
            "kotlin", "test.DefaultPackageKt", workDirectory = tmpdir, expectedExitCode = 1,
            expectedStderr = """
            error: could not find or load main class test.DefaultPackageKt
            Caused by: java.lang.NoClassDefFoundError: test/DefaultPackageKt (wrong name: DefaultPackageKt)

        """.trimIndent()
        )
    }

    fun testNoClassDefFoundErrorWhenClassNotInDefaultPackage() {
        val testDir = File("$tmpdir/test")

        runProcess("kotlinc", "$testDataDirectory/helloWorld.kt", "-d", tmpdir.path)
        assertExists(File("${testDir.path}/HelloWorldKt.class"))

        runProcess(
            "kotlin", "HelloWorldKt", workDirectory = testDir, expectedExitCode = 1,
            expectedStderr = """
            error: could not find or load main class HelloWorldKt
            Caused by: java.lang.NoClassDefFoundError: HelloWorldKt (wrong name: test/HelloWorldKt)

        """.trimIndent()
        )
    }

    /**
     * A class whose full qualified name is `DefaultPackageKt` and is located in path `$tmpdir/test/DefaultPackageKt.class`
     */
    fun testRunClassFileWithExtensionInDefaultPackage() {
        val subDir = File("$tmpdir/test/sub").apply { mkdirs() }
        val testDir = File("$tmpdir/test")

        runProcess("kotlinc", "$testDataDirectory/defaultPackage.kt", "-d", testDir.path)
        assertExists(File("${testDir.path}/DefaultPackageKt.class"))

        runProcess(
            "kotlin", "test/DefaultPackageKt.class", workDirectory = tmpdir, expectedExitCode = 1,
            expectedStderr = """
            error: could not find or load main class test.DefaultPackageKt
            Caused by: java.lang.NoClassDefFoundError: test/DefaultPackageKt (wrong name: DefaultPackageKt)
            
        """.trimIndent()
        )

        runProcess("kotlin", "DefaultPackageKt.class", expectedStdout = "ok", workDirectory = testDir)
        runProcess("kotlin", "./sub/../DefaultPackageKt.class", expectedStdout = "ok", workDirectory = testDir)