Skip to content

Commit

Permalink
#98 Changes, examples and drafts from test work shop (#99)
Browse files Browse the repository at this point in the history
We together a series of commits for moving to rspec testing, updating some code to be easier to test and added new tests as examples.

Commits:
* Added example test for #98. Run with rspec --format=doc
* #98 unit test and refactor example
* Added command line test for #98
* #98 More rspec test examples
* Updated gem files for test. See #98
* Added other test gem dependencies
  • Loading branch information
Bue Petersen authored May 1, 2017
1 parent e0c3a59 commit 02e0459
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ default.md
default_ids.md
default.pdf
default-generated.*

.bundle
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ COPY Gemfile.lock /usr/src/app/

#Ruby knows best how to install this particular version of PAC
#This means that this dockerfile can build any version of PAC.
RUN bundle install
RUN bundle install --without=test_gems

COPY . /usr/src/app

Expand Down
11 changes: 8 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ gem 'trac4r', :require => false
gem 'docopt'
gem 'ruby-fogbugz', :require => false
gem 'flexmock'
gem 'simplecov', :require => false
gem 'simplecov-rcov', :require => false
gem 'ci_reporter_test_unit'
gem 'zip'
gem 'liquid'
gem 'xml-simple', '~> 1.1', '>= 1.1.5'

group :test_gems do
gem 'rspec'
gem 'simplecov'
gem 'simplecov-rcov'
gem 'ci_reporter_test_unit'
end

15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GEM
safe_yaml (~> 1.0.0)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
docile (1.1.5)
docopt (0.5.0)
faraday (0.9.2)
Expand Down Expand Up @@ -69,6 +70,19 @@ GEM
rake (11.1.2)
rdoc (4.2.2)
json (~> 1.4)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
ruby-fogbugz (0.2.1)
crack (~> 0.4)
rugged (0.24.0)
Expand Down Expand Up @@ -110,6 +124,7 @@ DEPENDENCIES
mercurial-ruby
pdfkit
rake
rspec
ruby-fogbugz
rugged (~> 0.24.0)
simplecov
Expand Down
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ task :functional_vcs do
end
end

#Example
task :rspec do
`rspec --format='html' --out='results.html'`
end

task :coverage do
ENV['COVERAGE'] = 'on'
Rake::Task['test'].execute
Expand Down
32 changes: 32 additions & 0 deletions lib/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@

module Core extend self

def cli_text(file)
cli = <<DOCOPT
Usage:
#{file} from <oldest-ref> [--to=<newest-ref>] [options] [-v...] [-q...] [-c (<user> <password> <target>)]...
#{file} from-latest-tag <approximation> [--to=<newest-ref>] [options] [-v...] [-q...] [-c <user> <password> <target>]...
#{file} -h|--help
Options:
-h --help Show this screen.
--from <oldest-ref> Specify where to stop searching for commit. For git this takes anything that rev-parse accepts. Such as HEAD~3 / Git sha or tag name.
--from-latest-tag Looks for the newest commit that the tag with <approximation> points to.
--settings=<path> Path to the settings file used. If nothing is specified default_settings.yml is used
--properties=<properties>
Allows you to pass in additional variables to the Liquid templates. Must be in JSON format. Namespaced under properties.* in
your Liquid templates. Referenced like so '{{properties.[your-variable]}}' in your templates.
JSON keys and values should be wrapped in quotation marks '"' like so: --properties='{ "title":"PAC Changelog" }'
-v More verbose output. Can be repeated to increase output verbosity or to cancel out -q
-q Less verbose output. Can be repeated for more silence or to cancel out -v
-c Override username and password. Example: `-c my_user my_password jira`. This will set username and password for task system jira.
DOCOPT
cli
end

def settings
if defined?(@@settings).nil?
{}
Expand Down
107 changes: 50 additions & 57 deletions lib/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@ module Model
class PACTask
def initialize(task_id = nil)
#Lookup key for task management system
@task_id = task_id
#We assume that task_id is a string
raise ArgumentError.new("PACTask init error, task id is not a string.") if not (task_id == nil or task_id.respond_to?(:to_str))
@task_id = task_id
#Commits tied to this task
@commit_collection = PACCommitCollection.new
@commit_collection = PACCommitCollection.new
#Data from task management systems(s)
@attributes = { }
#Key that determines which system(s) we need to look in for data
@applies_to = Set.new
@applies_to = Set.new
#Assigned label. Used in templates so that you can group your tasks using labels.
@label = Set.new
end

def applies_to
@applies_to
end

def data
@data
# Initialized with nil - will be populated by decorators
# FIXME is it default nil ?
@data = nil
end

attr_accessor :commit_collection
attr_accessor :task_id
attr_accessor :data
attr_reader :label
attr_reader :applies_to
attr_reader :attributes
attr_accessor :commits

# accepts string
def applies_to=(val)
raiseArgumentError.new("apllies_to method accepts only a string") if not val.is_a? String
@applies_to << val
end

def add_commit(commit)
@commit_collection.add(commit)
end

def commits
@commit_collection.commits
def attributes=(value)
@attributes = value
end

def to_s
@task_id
end

def label
@label
@task_id
end

#Apppend label to the set of existing labels. This prevents duplicate labels.
Expand All @@ -54,31 +58,20 @@ def clear_labels

#We need to override the equals method. A task is uniquely identified by it's identifier (id). This is usually what
#is used to fetch additonal information from the task management system
def ==(other)
def ==(other)
@task_id == other.task_id
end

def to_liquid
hash = {
'task_id' => @task_id,
'commits' => @commit_collection,
hash = {
'task_id' => @task_id,
'commits' => @commit_collection,
'attributes' => attributes,
'label' => label,
'data' => data
}
hash
end

def attributes
@attributes
end

def attributes=(value)
@attributes = value
end

attr_accessor :commit_collection
attr_accessor :task_id
end

#Model object representing a collection of tasks. Includes logic to append commits to existing tasks.
Expand All @@ -88,22 +81,22 @@ def initialize
@tasks = []
end

#When you add a task to a collection. It will automatically add new commits to an existing task. You will
#When you add a task to a collection. It will automatically add new commits to an existing task. You will
#not get duplicate tasks in the collection.
def add(*t)
t.flatten.each do |task|
t.flatten.each do |task|
unless @tasks.include? task
@tasks << task
else
task.commit_collection.each do |c|
@tasks[@tasks.index(task)].commit_collection.add(c)
end
@tasks[@tasks.index(task)].commit_collection.add(c)
end
end
end
end

#Enumeartion method. So that PACTaskCollection.each yields a PACTask object
def each
def each
@tasks.each { |task| yield task }
end

Expand All @@ -113,7 +106,7 @@ def length

def unreferenced_commits
output = @tasks.select { |t| t.task_id.nil? }
if output.first.nil?
if output.first.nil?
[]
else
output.first.commits
Expand All @@ -125,14 +118,14 @@ def referenced_tasks
end

#Indexer method for each collected task
def [](task_id)
def [](task_id)
@tasks.select {|t| t.task_id == task_id}.first
end

#Group each task discovered by it's label
def by_label
labelled = Hash.new
@tasks.each { |t|
@tasks.each { |t|
t.label.each { |t_label|
unless labelled.include? t_label
labelled[t_label] = []
Expand All @@ -145,7 +138,7 @@ def by_label
labelled
end

def to_liquid
def to_liquid
liquid_hash = { 'tasks' => @tasks, 'referenced' => referenced_tasks, 'unreferenced' => unreferenced_commits }
liquid_hash.merge! by_label
liquid_hash
Expand All @@ -155,7 +148,7 @@ def to_liquid
end

class PACCommitCollection
def initialize
def initialize
@commits = []
end

Expand All @@ -180,41 +173,41 @@ def count_without
@commits.select{ |c| c.referenced == false }.size
end

def health
def health
count_with.to_f / count
end

def to_liquid
def to_liquid
@commits
end

attr_accessor :commits
attr_accessor :commits
end

class PACCommit
def initialize(sha, message = nil, date = nil)
@sha = sha
@message = message
@referenced = false
@date = date
@date = date
end

def to_liquid
{
def to_liquid
{
'sha' => @sha,
'message' => @message,
'header' => header,
'message' => @message,
'header' => header,
'referenced' => referenced,
'shortsha' => shortsha
}
end

def ==(other)
@sha == other.sha
def ==(other)
@sha == other.sha
end

def header
@message.split(/\n/).first
def header
@message.split(/\n/).first
end

#Get default abbreviation from Git
Expand All @@ -226,7 +219,7 @@ def shortsha(n = 7)
#returned for unmatch commits. That is a PACTask with an id of 'nil'.
def matchtask(regex, split = nil)
tasks = []
regex.each do |r|
regex.each do |r|
@message.scan(eval(r['pattern'])).each do |arr|
if split.nil?
task = PACTask.new(arr[0])
Expand All @@ -240,12 +233,12 @@ def matchtask(regex, split = nil)
task.add_commit(self)
task.label = r['label']
self.referenced = true
tasks << task
tasks << task
end
end
end
end

tasks
end

Expand Down
Loading

0 comments on commit 02e0459

Please sign in to comment.