Skip to content

Commit 9f8d82f

Browse files
authored
Merge pull request #154 from ruby-syntax-tree/fix-ci
More attempts to fix CI
2 parents 75cc00a + 3708b7c commit 9f8d82f

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

.github/workflows/gh-pages.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: Github Pages (rdoc)
2-
on: [push]
2+
on:
3+
push:
4+
branches:
5+
- main
6+
37
jobs:
48
build-and-deploy:
59
runs-on: ubuntu-latest

lib/syntax_tree/cli.rb

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ def handler
5151
def source
5252
handler.read(filepath)
5353
end
54+
55+
def writable?
56+
File.writable?(filepath)
57+
end
5458
end
5559

5660
# An item of work that corresponds to a script content passed via the
5761
# command line.
5862
class ScriptItem
59-
FILEPATH = :script
60-
6163
attr_reader :source
6264

6365
def initialize(source)
@@ -69,7 +71,30 @@ def handler
6971
end
7072

7173
def filepath
72-
FILEPATH
74+
:script
75+
end
76+
77+
def writable?
78+
false
79+
end
80+
end
81+
82+
# An item of work that correspond to the content passed in via stdin.
83+
class STDINItem
84+
def handler
85+
HANDLERS[".rb"]
86+
end
87+
88+
def filepath
89+
:stdin
90+
end
91+
92+
def source
93+
$stdin.read
94+
end
95+
96+
def writable?
97+
false
7398
end
7499
end
75100

@@ -196,7 +221,7 @@ def run(item)
196221

197222
source = item.source
198223
formatted = item.handler.format(source, options.print_width)
199-
File.write(filepath, formatted) if item.filepath != :script
224+
File.write(filepath, formatted) if item.writable?
200225

201226
color = source == formatted ? Color.gray(filepath) : filepath
202227
delta = ((Time.now - start) * 1000).round
@@ -386,7 +411,7 @@ def run(argv)
386411
return 1
387412
end
388413

389-
# If we're not reading from stdin and the user didn't supply and
414+
# If we're not reading from stdin and the user didn't supply any
390415
# filepaths to be read, then we exit with the usage message.
391416
if $stdin.tty? && arguments.empty? && options.scripts.empty?
392417
warn(HELP)
@@ -403,12 +428,13 @@ def run(argv)
403428
Dir
404429
.glob(pattern)
405430
.each do |filepath|
406-
queue << FileItem.new(filepath) if File.file?(filepath)
431+
queue << FileItem.new(filepath) if File.readable?(filepath)
407432
end
408433
end
434+
409435
options.scripts.each { |script| queue << ScriptItem.new(script) }
410436
else
411-
queue << ScriptItem.new($stdin.read)
437+
queue << STDINItem.new
412438
end
413439

414440
# At the end, we're going to return whether or not this worker ever

test/cli_test.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_help_default
117117
end
118118

119119
def test_no_arguments
120-
$stdin.stub(:tty?, true) do
120+
with_tty do
121121
*, stderr = capture_io { SyntaxTree::CLI.run(["check"]) }
122122
assert_includes(stderr, "stree help")
123123
end
@@ -134,13 +134,17 @@ def test_no_arguments_no_tty
134134
end
135135

136136
def test_inline_script
137-
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) }
138-
assert_equal("1 + 1\n", stdio)
137+
with_tty do
138+
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1]) }
139+
assert_equal("1 + 1\n", stdio)
140+
end
139141
end
140142

141143
def test_multiple_inline_scripts
142-
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) }
143-
assert_equal("1 + 1\n2 + 2\n", stdio)
144+
with_tty do
145+
stdio, = capture_io { SyntaxTree::CLI.run(%w[format -e 1+1 -e 2+2]) }
146+
assert_equal("1 + 1\n2 + 2\n", stdio)
147+
end
144148
end
145149

146150
def test_generic_error
@@ -241,8 +245,10 @@ def run_cli(command, *args, contents: :default)
241245

242246
status = nil
243247
stdio, stderr =
244-
capture_io do
245-
status = SyntaxTree::CLI.run([command, *args, tempfile.path])
248+
with_tty do
249+
capture_io do
250+
status = SyntaxTree::CLI.run([command, *args, tempfile.path])
251+
end
246252
end
247253

248254
Result.new(status: status, stdio: stdio, stderr: stderr)
@@ -251,6 +257,10 @@ def run_cli(command, *args, contents: :default)
251257
tempfile.unlink
252258
end
253259

260+
def with_tty(&block)
261+
$stdin.stub(:tty?, true, &block)
262+
end
263+
254264
def with_config_file(contents)
255265
filepath = File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
256266
File.write(filepath, contents)

0 commit comments

Comments
 (0)