Skip to content

Commit

Permalink
Introducing Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Feb 7, 2022
1 parent 6ccee1b commit 2237db4
Show file tree
Hide file tree
Showing 28 changed files with 280 additions and 144 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
bundle config set --local without benchmark
bundle install --jobs=3
- name: Rubocop
run: bundle exec rubocop

- name: Tests
run: bundle exec rspec

Expand Down
102 changes: 102 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# The behavior of RuboCop can be controlled via the .rubocop.yml
# configuration file. It makes it possible to enable/disable
# certain cops (checks) and to alter their behavior if they accept
# any parameters. The file can be placed either in your home
# directory or in some project directory.
#
# RuboCop will start looking for the configuration file in the directory
# where the inspected file is and continue its way up to the root directory.
#
# See https://docs.rubocop.org/rubocop/configuration


AllCops:
NewCops: disable
Exclude:
- "spec/speculations/**/*.rb" # Generated by speculate_about
SuggestExtensions: false
TargetRubyVersion: 3.1

Layout/EmptyLinesAroundAccessModifier:
Enabled: false

Layout/EmptyLinesAroundAttributeAccessor:
Enabled: false

Layout/ExtraSpacing:
Enabled: false

Layout/IndentationConsistency:
Enabled: false

Layout/SpaceAroundOperators:
Enabled: false

Layout/SpaceBeforeBlockBraces:
Enabled: false

# Metrics/AbcSize:
# Exclude:

Metrics/BlockLength:
Exclude:
- "spec/**/*.rb"

Metrics/MethodLength:
Max: 15

Naming/HeredocDelimiterNaming:
Enabled: false

Naming/MemoizedInstanceVariableName:
Enabled: false

Naming/MethodName:
Enabled: false

Naming/MethodParameterName:
Enabled: false

Style/BlockComments:
Exclude:
- "spec/spec_helper.rb"

Style/CaseEquality:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false

Style/Documentation:
Enabled: false

Style/DoubleNegation:
Enabled: false

Style/EmptyCaseCondition:
Enabled: false

# Style/EmptyMethod:
# Enabled: false

Style/FrozenStringLiteralComment:
Include:
- "lib/**/*.rb"

Style/Lambda:
Enabled: false

Style/LambdaCall:
Enabled: false

Style/ModuleFunction:
Enabled: false

Style/StringLiterals:
Enabled: false

Style/TrailingBodyOnModule:
Enabled: false

Style/WhileUntilModifier:
Enabled: false
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gemspec

group :development, :test do
gem "debug"
gem "pry-byebug"
gem "rspec"
gem "rubocop", require: false
gem "simplecov", require: false
gem 'simplecov-lcov', '~> 0.8.0'
gem "rspec"
end
# SPDX-License-Identifier: Apache-2.0
21 changes: 21 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
byebug (11.1.3)
coderay (1.1.3)
debug (1.4.0)
Expand All @@ -17,14 +18,20 @@ GEM
irb (1.4.1)
reline (>= 0.3.0)
method_source (1.0.0)
parallel (1.21.0)
parser (3.1.0.0)
ast (~> 2.4.1)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.9.0)
byebug (~> 11.0)
pry (~> 0.13.0)
rainbow (3.1.1)
regexp_parser (2.2.0)
reline (0.3.1)
io-console (~> 0.5)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
Expand All @@ -38,13 +45,26 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.3)
rubocop (1.25.1)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.15.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.1)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.3)
unicode-display_width (2.1.0)

PLATFORMS
x86_64-darwin-21
Expand All @@ -54,6 +74,7 @@ DEPENDENCIES
debug
pry-byebug
rspec
rubocop
simplecov
simplecov-lcov (~> 0.8.0)
speculate_about!
Expand Down
8 changes: 5 additions & 3 deletions lib/speculate_about.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# frozen_string_literal: true

require 'rspec'
require_relative 'speculations/parser'
module SpeculateAbout

def speculate_about(infile)
raise ArgumentError, "#{infile} not found" unless File.readable? infile

ast = Speculations::Parser.new.parse_from_file(infile)
code = ast.to_code.join("\n")
ENV["SPECULATE_ABOUT_DEBUG"] ? _show(code, infile) : instance_eval(code, infile)
end

private
def _show(code, path)
message = "Generated code for #{path}"
message = "Generated code for #{path}"
_underline(message)
puts code
end

def _underline(message, ul: "=")
puts message
puts message.gsub(/./, ul)
end

end

RSpec.configure do |conf|
Expand Down
33 changes: 18 additions & 15 deletions lib/speculations.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

require 'fileutils'
require_relative 'speculations/parser'
module Speculations extend self
DISCLAIMER = <<~EOD
# DO NOT EDIT!!!
# This file was generated from FILENAME with the speculate_about gem, if you modify this file
# one of two bad things will happen
# - your documentation specs are not correct
# - your modifications will be overwritten by the speculate command line
# YOU HAVE BEEN WARNED
EOD

DISCLAIMER = <<-EOD
# DO NOT EDIT!!!
# This file was generated from FILENAME with the speculate_about gem, if you modify this file
# one of two bad things will happen
# - your documentation specs are not correct
# - your modifications will be overwritten by the speculate command line
# YOU HAVE BEEN WARNED
EOD

def compile(infile, outfile=nil)
def compile(infile, outfile = nil)
raise ArgumentError, "#{infile} not found" unless File.readable? infile

outfile ||= _speculation_path(infile)
if _out_of_date?(outfile, infile)
ast = Speculations::Parser.new.parse_from_file(infile)
Expand All @@ -24,14 +26,15 @@ def compile(infile, outfile=nil)

private

def _decorated_ast_code ast, filename
[DISCLAIMER.gsub("FILENAME", filename.inspect).split("\n"), %{RSpec.describe #{filename.inspect} do}] +
ast.to_code + ["end"]
def _decorated_ast_code(ast, filename)
[DISCLAIMER.gsub("FILENAME", filename.inspect).split("\n"), %(RSpec.describe #{filename.inspect} do)] +
ast.to_code + ["end"]
end

def _out_of_date?(outf, inf)
return true unless File.exists? outf
return File.lstat(outf).mtime <= File.lstat(inf).mtime
return true unless File.exist? outf

File.lstat(outf).mtime <= File.lstat(inf).mtime
end

def _speculation_path(file)
Expand Down
15 changes: 8 additions & 7 deletions lib/speculations/cli.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

module Speculations
module CLI extend self
def run args
def run(args)
loop do
case args.first
case args.first
when "-h", "--help"
_usage
when "-v", "--version"
Expand All @@ -16,7 +17,7 @@ def run args

private

def _compile_and_maybe_run args
def _compile_and_maybe_run(args)
require_relative "../speculations"
args = Dir.glob(["*.md", "speculations/**/*.md"]) if args.empty?
args.each do |input_file|
Expand All @@ -27,23 +28,23 @@ def _compile_and_maybe_run args
def _usage
puts <<-EOF
usage:
#{$0} [options] [filenames]
#{$PROGRAM_NAME} [options] [filenames]
options:
-h | --help display this exit with -1
-v | --version display version of the speculate_about gem exit with -2
filenames (default to all markdown files in the project directory and its speculations subdirectories)
recreate outdated speculations in `spec/speculations/`
recreate outdated speculations in `spec/speculations/`#{' '}
EOF
exit -1
exit(-1)
end

def _version
require_relative 'version'
puts VERSION
exit -2
exit(-2)
end
end
end
11 changes: 6 additions & 5 deletions lib/speculations/parser.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

module Speculations
class Parser
require_relative './parser/context'
require_relative './parser/state'


def self.parsers
@__parsers__ ||= {
candidate: State::Candidate,
Expand All @@ -13,12 +14,12 @@ def self.parsers
}
end

def parse_from_file file
def parse_from_file(file)
@filename = file
@input = File
.new(file)
.each_line(chomp: true)
.lazy
.new(file)
.each_line(chomp: true)
.lazy
parse!
end

Expand Down

0 comments on commit 2237db4

Please sign in to comment.