aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt
blob: 9f0919bfeefdcd3cd551386b4e93f39eff067353 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.ktfmt.cli

import com.facebook.ktfmt.format.Formatter
import com.facebook.ktfmt.format.FormattingOptions
import com.google.common.truth.Truth.assertThat
import java.io.ByteArrayOutputStream
import java.io.FileNotFoundException
import java.io.PrintStream
import kotlin.io.path.createTempDirectory
import kotlin.test.assertFailsWith
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@Suppress("FunctionNaming")
@RunWith(JUnit4::class)
class ParsedArgsTest {

  private val root = createTempDirectory().toFile()

  @After
  fun tearDown() {
    root.deleteRecursively()
  }

  @Test
  fun `files to format are returned and unknown flags are reported`() {
    val (parsed, out) = parseTestOptions("foo.kt", "--unknown")

    assertThat(parsed.fileNames).containsExactly("foo.kt")
    assertThat(out.toString()).isEqualTo("Unexpected option: --unknown\n")
  }

  @Test
  fun `files to format are returned and flags starting with @ are reported`() {
    val (parsed, out) = parseTestOptions("foo.kt", "@unknown")

    assertThat(parsed.fileNames).containsExactly("foo.kt")
    assertThat(out.toString()).isEqualTo("Unexpected option: @unknown\n")
  }

  @Test
  fun `parseOptions uses default values when args are empty`() {
    val (parsed, _) = parseTestOptions("foo.kt")

    val formattingOptions = parsed.formattingOptions
    assertThat(formattingOptions.style).isEqualTo(FormattingOptions.Style.FACEBOOK)
    assertThat(formattingOptions.maxWidth).isEqualTo(100)
    assertThat(formattingOptions.blockIndent).isEqualTo(2)
    assertThat(formattingOptions.continuationIndent).isEqualTo(4)
    assertThat(formattingOptions.removeUnusedImports).isTrue()
    assertThat(formattingOptions.debuggingPrintOpsAfterFormatting).isFalse()

    assertThat(parsed.dryRun).isFalse()
    assertThat(parsed.setExitIfChanged).isFalse()
    assertThat(parsed.stdinName).isNull()
  }

  @Test
  fun `parseOptions recognizes --dropbox-style and rejects unknown flags`() {
    val (parsed, out) = parseTestOptions("--dropbox-style", "foo.kt", "--unknown")

    assertThat(parsed.fileNames).containsExactly("foo.kt")
    assertThat(parsed.formattingOptions.blockIndent).isEqualTo(4)
    assertThat(parsed.formattingOptions.continuationIndent).isEqualTo(4)
    assertThat(out.toString()).isEqualTo("Unexpected option: --unknown\n")
  }

  @Test
  fun `parseOptions recognizes --google-style`() {
    val (parsed, _) = parseTestOptions("--google-style", "foo.kt")
    assertThat(parsed.formattingOptions).isEqualTo(Formatter.GOOGLE_FORMAT)
  }

  @Test
  fun `parseOptions recognizes --dry-run`() {
    val (parsed, _) = parseTestOptions("--dry-run", "foo.kt")
    assertThat(parsed.dryRun).isTrue()
  }

  @Test
  fun `parseOptions recognizes -n as --dry-run`() {
    val (parsed, _) = parseTestOptions("-n", "foo.kt")
    assertThat(parsed.dryRun).isTrue()
  }

  @Test
  fun `parseOptions recognizes --set-exit-if-changed`() {
    val (parsed, _) = parseTestOptions("--set-exit-if-changed", "foo.kt")
    assertThat(parsed.setExitIfChanged).isTrue()
  }

  @Test
  fun `parseOptions defaults to removing imports`() {
    val (parsed, _) = parseTestOptions("foo.kt")
    assertThat(parsed.formattingOptions.removeUnusedImports).isTrue()
  }

  @Test
  fun `parseOptions recognizes --do-not-remove-unused-imports to removing imports`() {
    val (parsed, _) = parseTestOptions("--do-not-remove-unused-imports", "foo.kt")
    assertThat(parsed.formattingOptions.removeUnusedImports).isFalse()
  }

  @Test
  fun `parseOptions handles dropbox style and --do-not-remove-unused-imports`() {
    val (parsed, _) =
        parseTestOptions("--do-not-remove-unused-imports", "--dropbox-style", "foo.kt")
    assertThat(parsed.formattingOptions.removeUnusedImports).isFalse()
    assertThat(parsed.formattingOptions.style).isEqualTo(FormattingOptions.Style.DROPBOX)
  }

  @Test
  fun `parseOptions handles google style and --do-not-remove-unused-imports`() {
    val (parsed, _) = parseTestOptions("--do-not-remove-unused-imports", "--google-style", "foo.kt")
    assertThat(parsed.formattingOptions.removeUnusedImports).isFalse()
    assertThat(parsed.formattingOptions.style).isEqualTo(FormattingOptions.Style.GOOGLE)
  }

  @Test
  fun `parseOptions --stdin-name`() {
    val (parsed, _) = parseTestOptions("--stdin-name=my/foo.kt")
    assertThat(parsed.stdinName).isEqualTo("my/foo.kt")
  }

  @Test
  fun `parseOptions --stdin-name with empty value`() {
    val (parsed, _) = parseTestOptions("--stdin-name=")
    assertThat(parsed.stdinName).isEqualTo("")
  }

  @Test
  fun `parseOptions --stdin-name without value`() {
    val (parsed, out) = parseTestOptions("--stdin-name")
    assertThat(out).isEqualTo("Found option '--stdin-name', expected '--stdin-name=<value>'\n")
    assertThat(parsed.stdinName).isNull()
  }

  @Test
  fun `parseOptions --stdin-name prefix`() {
    val (parsed, out) = parseTestOptions("--stdin-namea")
    assertThat(out).isEqualTo("Found option '--stdin-namea', expected '--stdin-name=<value>'\n")
    assertThat(parsed.stdinName).isNull()
  }

  @Test
  fun `processArgs use the @file option with non existing file`() {
    val out = ByteArrayOutputStream()

    val e =
        assertFailsWith<FileNotFoundException> {
          ParsedArgs.processArgs(PrintStream(out), arrayOf("@non-existing-file"))
        }
    assertThat(e.message).contains("non-existing-file (No such file or directory)")
  }

  @Test
  fun `processArgs use the @file option with file containing arguments`() {
    val out = ByteArrayOutputStream()
    val file = root.resolve("existing-file")
    file.writeText("--google-style\n--dry-run\n--set-exit-if-changed\nFile1.kt\nFile2.kt\n")

    val parsed = ParsedArgs.processArgs(PrintStream(out), arrayOf("@" + file.absolutePath))

    assertThat(parsed.formattingOptions).isEqualTo(Formatter.GOOGLE_FORMAT)
    assertThat(parsed.dryRun).isTrue()
    assertThat(parsed.setExitIfChanged).isTrue()
    assertThat(parsed.fileNames).containsExactlyElementsIn(listOf("File1.kt", "File2.kt"))
  }

  private fun parseTestOptions(vararg args: String): Pair<ParsedArgs, String> {
    val out = ByteArrayOutputStream()
    return Pair(ParsedArgs.parseOptions(PrintStream(out), arrayOf(*args)), out.toString())
  }
}