Skip to content

Commit c571bed

Browse files
committed
refactor static analysis checks
1 parent 711fd98 commit c571bed

File tree

10 files changed

+93
-198
lines changed

10 files changed

+93
-198
lines changed

.github/workflows/static-analysis.yml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Setup Ruby
3131
uses: ruby/setup-ruby@v1
3232
with:
33-
ruby-version: '3.1'
33+
ruby-version: '3.2'
3434
bundler-cache: true
3535

3636
- name: Generate all examples
@@ -39,17 +39,8 @@ jobs:
3939
- name: Generate all fixtures
4040
run: bundle exec run fixtures regen
4141

42-
- name: Run shellcheck tests (examples)
43-
run: bundle exec run shellcheck examples
44-
45-
- name: Run shellcheck tests (fixtures)
46-
run: bundle exec run shellcheck fixtures
47-
48-
- name: Run shfmt tests (examples)
49-
run: bundle exec run shfmt examples
50-
51-
- name: Run shfmt tests (fixtures)
52-
run: bundle exec run shfmt fixtures
42+
- name: Run shellcheck and shfmt tests
43+
run: bundle exec run static
5344

5445
json_schema:
5546
name: Validate JSON schemas

runfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'debug'
22
require 'bashly/version'
33
require_relative 'support/runfile/example'
4-
require_relative 'support/runfile/workspace_fixture'
54

65
title 'Bashly Developer Toolbelt'
76
summary 'Runfile tasks for building the Bashly gem'

support/runfile/example.rb

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,43 @@
33
# A helper class used in Runfile to generate example README files
44
class Example
55
class << self
6-
def dirs
7-
@dirs ||= Dir['examples/*'].select { |f| File.directory? f }
6+
def examples
7+
filter_dirs('examples').map { |dir| new dir }
8+
end
9+
10+
def fixtures
11+
filter_dirs('spec/fixtures/workspaces').map { |dir| new dir, type: :fixture }
812
end
913

1014
def all
11-
dirs.map { |dir| Example.new dir }
15+
examples + fixtures
1216
end
1317

14-
def executables
15-
all.map(&:executable)
18+
private
19+
20+
def filter_dirs(base_dir)
21+
Dir["#{base_dir}/*"].select { |f| File.directory? f }
1622
end
23+
1724
end
1825

19-
attr_reader :dir
26+
attr_reader :dir, :type
2027

21-
def initialize(dir)
28+
def initialize(dir, type: :example)
2229
@dir = dir
30+
@type = type
31+
end
32+
33+
def inspect
34+
%Q[#<Example type=:#{type}, dir="#{dir}">]
2335
end
2436

2537
def config
26-
@config ||= YAML.unsafe_load_file yaml_path
38+
@config ||= if File.exist? yaml_path
39+
YAML.unsafe_load_file yaml_path
40+
else
41+
{}
42+
end
2743
end
2844

2945
def yaml
@@ -34,12 +50,27 @@ def yaml_path
3450
"#{dir}/src/bashly.yml"
3551
end
3652

53+
def readme
54+
@readme ||= File.exist?(readme_path) ? File.read(readme_path) : ''
55+
end
56+
3757
def readme_path
3858
"#{dir}/README.md"
3959
end
4060

41-
def readme
42-
File.read readme_path
61+
def regenerate_readme
62+
raise "#regenerate_readme called on a fixture" if type == :fixture
63+
File.write readme_path, generated_readme
64+
end
65+
66+
def executable
67+
@executable ||= File.file?(executable_path) ? executable_path : nil
68+
end
69+
70+
private
71+
72+
def executable_path
73+
"#{dir}/#{config['name']}"
4374
end
4475

4576
def test_commands
@@ -72,10 +103,6 @@ def test_output
72103
result
73104
end
74105

75-
def regenerate_readme
76-
File.write readme_path, generated_readme
77-
end
78-
79106
def generated_readme
80107
marker = '-----'
81108
content = readme.split(marker)[0].strip
@@ -135,8 +162,4 @@ def langs
135162
'.yaml' => 'yaml',
136163
}
137164
end
138-
139-
def executable
140-
"#{dir}/#{config['name']}"
141-
end
142165
end

support/runfile/examples.runfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ usage "regen [FILTER]"
55
action :regen do |args|
66
# Patch the PATH to allow the extensible example to run properly
77
ENV['PATH']="#{Dir.pwd}/examples/extensible:#{ENV['PATH']}"
8-
Example.all.each do |example|
8+
Example.examples.each do |example|
99
if args['FILTER']
1010
next unless example.dir.include? ENV['FILTER']
1111
end
@@ -24,7 +24,7 @@ action :readme do |args|
2424

2525
# Patch the PATH to allow the extensible example to run properly
2626
ENV['PATH']="#{Dir.pwd}/examples/extensible:#{ENV['PATH']}"
27-
Example.all.each do |example|
27+
Example.examples.each do |example|
2828
if args['FILTER']
2929
next unless example.dir.include? args['FILTER']
3030
end

support/runfile/fixtures.runfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

support/runfile/schema.runfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ action(:invalid) { check_invalid }
5656
helpers do
5757
def check_examples
5858
say "\ngub`Examples`"
59-
Example.all.each do |example|
59+
Example.examples.each do |example|
6060
file = example.yaml_path
6161
schema_check file
6262
end

support/runfile/shellcheck.runfile

Lines changed: 0 additions & 51 deletions
This file was deleted.

support/runfile/shfmt.runfile

Lines changed: 0 additions & 53 deletions
This file was deleted.

support/runfile/static.runfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
summary 'Run shellcheck and shfmt checks on all examples'
2+
3+
help "Run shellcheck and shfmt on all examples or a given example"
4+
usage "[FILTER]"
5+
action do |args|
6+
blacklist = %w[
7+
examples/render-mandoc
8+
examples/render-markdown
9+
examples/settings
10+
spec/fixtures/workspaces/completions-private
11+
spec/fixtures/workspaces/custom-paths
12+
spec/fixtures/workspaces/import
13+
spec/fixtures/workspaces/lib-upgrade
14+
]
15+
16+
errors = 0
17+
18+
Example.all.each do |example|
19+
if args['FILTER']
20+
next unless example.dir.include? args['FILTER']
21+
end
22+
23+
next if blacklist.include? example.dir
24+
25+
if !example.executable
26+
say "nb`WARNING: Skipping #{example.dir}`"
27+
next
28+
end
29+
30+
say "\n#{example.executable}"
31+
success = system "shellcheck #{example.executable}"
32+
say success ? 'g` ✓ shellcheck`' : ' r`✗ shellcheck`'
33+
errors += 1 unless success
34+
35+
success = system "shfmt -d -i 2 -ci #{example.executable}"
36+
say success ? ' g`✓ shfmt`' : ' r`✗ shfmt`'
37+
errors += 1 unless success
38+
end
39+
40+
if errors > 0
41+
say "\nrb`#{errors} errors found`"
42+
exit 1
43+
end
44+
45+
say "\nno errors found"
46+
end

support/runfile/workspace_fixture.rb

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)