Skip to content

Commit 1ea066b

Browse files
committed
Fix CLI checking for content
Previously, we were checking if $stdin was a TTY to determine if there was content to be read. As it turns out, this isn't really a good indicator, as content could always come later, and some folks run stree in CI when $stdin is not a TTY and still pass filenames. Instead, we now check if no filenames were passed, and in that case we attempt to read from $stdin.
1 parent 695918e commit 1ea066b

File tree

2 files changed

+9
-33
lines changed

2 files changed

+9
-33
lines changed

lib/syntax_tree/cli.rb

+3-10
Original file line numberDiff line numberDiff line change
@@ -423,19 +423,12 @@ def run(argv)
423423
return 1
424424
end
425425

426-
# If we're not reading from stdin and the user didn't supply any
427-
# filepaths to be read, then we exit with the usage message.
428-
if $stdin.tty? && arguments.empty? && options.scripts.empty?
429-
warn(HELP)
430-
return 1
431-
end
432-
433426
# We're going to build up a queue of items to process.
434427
queue = Queue.new
435428

436-
# If we're reading from stdin, then we'll just add the stdin object to
437-
# the queue. Otherwise, we'll add each of the filepaths to the queue.
438-
if $stdin.tty? && (arguments.any? || options.scripts.any?)
429+
# If there are any arguments or scripts, then we'll add those to the
430+
# queue. Otherwise we'll read the content off STDIN.
431+
if arguments.any? || options.scripts.any?
439432
arguments.each do |pattern|
440433
Dir
441434
.glob(pattern)

test/cli_test.rb

+6-23
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,6 @@ def test_help_default
123123
end
124124

125125
def test_no_arguments
126-
with_tty do
127-
*, stderr = capture_io { SyntaxTree::CLI.run(["check"]) }
128-
assert_includes(stderr, "stree help")
129-
end
130-
end
131-
132-
def test_no_arguments_no_tty
133126
stdin = $stdin
134127
$stdin = StringIO.new("1+1")
135128

@@ -140,17 +133,13 @@ def test_no_arguments_no_tty
140133
end
141134

142135
def test_inline_script
143-
with_tty do
144-
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) }
145-
assert_equal("1 + 1\n", stdio)
146-
end
136+
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) }
137+
assert_equal("1 + 1\n", stdio)
147138
end
148139

149140
def test_multiple_inline_scripts
150-
with_tty do
151-
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) }
152-
assert_equal("1 + 1\n2 + 2\n", stdio)
153-
end
141+
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) }
142+
assert_equal("1 + 1\n2 + 2\n", stdio)
154143
end
155144

156145
def test_generic_error
@@ -251,10 +240,8 @@ def run_cli(command, *args, contents: :default)
251240

252241
status = nil
253242
stdio, stderr =
254-
with_tty do
255-
capture_io do
256-
status = SyntaxTree::CLI.run([command, *args, tempfile.path])
257-
end
243+
capture_io do
244+
status = SyntaxTree::CLI.run([command, *args, tempfile.path])
258245
end
259246

260247
Result.new(status: status, stdio: stdio, stderr: stderr)
@@ -263,10 +250,6 @@ def run_cli(command, *args, contents: :default)
263250
tempfile.unlink
264251
end
265252

266-
def with_tty(&block)
267-
$stdin.stub(:tty?, true, &block)
268-
end
269-
270253
def with_config_file(contents)
271254
filepath = File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
272255
File.write(filepath, contents)

0 commit comments

Comments
 (0)