summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/unit/cli_spec.rb
blob: 23412f019386caab7e910cb5b517e647511a49bd (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# frozen_string_literal: true

require_relative "../spec_helper"

module SyntaxSuggest
  class FakeExit
    def initialize
      @called = false
      @value = nil
    end

    def exit(value = nil)
      @called = true
      @value = value
    end

    def called?
      @called
    end

    attr_reader :value
  end

  RSpec.describe Cli do
    it "parses valid code" do
      Dir.mktmpdir do |dir|
        dir = Pathname(dir)
        file = dir.join("script.rb")
        file.write("puts 'lol'")

        io = StringIO.new
        exit_obj = FakeExit.new
        Cli.new(
          io: io,
          argv: [file.to_s],
          exit_obj: exit_obj
        ).call

        expect(exit_obj.called?).to be_truthy
        expect(exit_obj.value).to eq(0)
        expect(io.string.strip).to eq("Syntax OK")
      end
    end

    it "parses invalid code" do
      file = fixtures_dir.join("this_project_extra_def.rb.txt")

      io = StringIO.new
      exit_obj = FakeExit.new
      Cli.new(
        io: io,
        argv: [file.to_s],
        exit_obj: exit_obj
      ).call

      out = io.string
      debug_display(out)

      expect(exit_obj.called?).to be_truthy
      expect(exit_obj.value).to eq(1)
      expect(out.strip).to include("> 36      def filename")
    end

    it "parses valid code with flags" do
      Dir.mktmpdir do |dir|
        dir = Pathname(dir)
        file = dir.join("script.rb")
        file.write("puts 'lol'")

        io = StringIO.new
        exit_obj = FakeExit.new
        cli = Cli.new(
          io: io,
          argv: ["--terminal", file.to_s],
          exit_obj: exit_obj
        )
        cli.call

        expect(exit_obj.called?).to be_truthy
        expect(exit_obj.value).to eq(0)
        expect(cli.options[:terminal]).to be_truthy
        expect(io.string.strip).to eq("Syntax OK")
      end
    end

    it "errors when no file given" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: ["--terminal"],
        exit_obj: exit_obj
      )
      cli.call

      expect(exit_obj.called?).to be_truthy
      expect(exit_obj.value).to eq(1)
      expect(io.string.strip).to eq("No file given")
    end

    it "errors when file does not exist" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: ["lol-i-d-o-not-ex-ist-yololo.txtblerglol"],
        exit_obj: exit_obj
      )
      cli.call

      expect(exit_obj.called?).to be_truthy
      expect(exit_obj.value).to eq(1)
      expect(io.string.strip).to include("file not found:")
    end

    # We cannot execute the parser here
    # because it calls `exit` and it will exit
    # our tests, however we can assert that the
    # parser has the right value for version
    it "-v version" do
      io = StringIO.new
      exit_obj = FakeExit.new
      parser = Cli.new(
        io: io,
        argv: ["-v"],
        exit_obj: exit_obj
      ).parser

      expect(parser.version).to include(SyntaxSuggest::VERSION.to_s)
    end

    it "SYNTAX_SUGGEST_RECORD_DIR" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: [],
        env: {"SYNTAX_SUGGEST_RECORD_DIR" => "hahaha"},
        exit_obj: exit_obj
      ).parse

      expect(exit_obj.called?).to be_falsey
      expect(cli.options[:record_dir]).to eq("hahaha")
    end

    it "--record-dir=<dir>" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: ["--record=lol"],
        exit_obj: exit_obj
      ).parse

      expect(exit_obj.called?).to be_falsey
      expect(cli.options[:record_dir]).to eq("lol")
    end

    it "terminal default to respecting TTY" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: [],
        exit_obj: exit_obj
      ).parse

      expect(exit_obj.called?).to be_falsey
      expect(cli.options[:terminal]).to eq(SyntaxSuggest::DEFAULT_VALUE)
    end

    it "--terminal" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: ["--terminal"],
        exit_obj: exit_obj
      ).parse

      expect(exit_obj.called?).to be_falsey
      expect(cli.options[:terminal]).to be_truthy
    end

    it "--no-terminal" do
      io = StringIO.new
      exit_obj = FakeExit.new
      cli = Cli.new(
        io: io,
        argv: ["--no-terminal"],
        exit_obj: exit_obj
      ).parse

      expect(exit_obj.called?).to be_falsey
      expect(cli.options[:terminal]).to be_falsey
    end

    it "--help outputs help" do
      io = StringIO.new
      exit_obj = FakeExit.new
      Cli.new(
        io: io,
        argv: ["--help"],
        exit_obj: exit_obj
      ).call

      expect(exit_obj.called?).to be_truthy
      expect(io.string).to include("Usage: syntax_suggest <file> [options]")
    end

    it "<empty args> outputs help" do
      io = StringIO.new
      exit_obj = FakeExit.new
      Cli.new(
        io: io,
        argv: [],
        exit_obj: exit_obj
      ).call

      expect(exit_obj.called?).to be_truthy
      expect(io.string).to include("Usage: syntax_suggest <file> [options]")
    end
  end
end