Permalink
Browse files

gotta create some fixtures

  • Loading branch information...
1 parent 06f9d96 commit 34d07884b49574ed9d11c73a34c5f1e71a41f2e6 @cowboy committed Aug 18, 2011
Showing with 192 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +3 −2 gaucho.gemspec
  3. +136 −0 spec/fixtures_helper.rb
  4. +9 −0 spec/gaucho/pageset_spec.rb
  5. +43 −0 spec/generate_fixtures.rb
  6. 0 spec/spec_helper.rb
View
1 .gitignore
@@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
+spec/fixtures/*
View
5 gaucho.gemspec
@@ -6,14 +6,15 @@ Gem::Specification.new do |s|
s.name = "gaucho"
s.version = Gaucho::VERSION
s.platform = Gem::Platform::RUBY
- s.authors = ["TODO: Write your name"]
+ s.authors = ['"Cowboy" Ben Alman']
s.email = ["TODO: Write your email address"]
s.homepage = ""
s.summary = %q{TODO: Write a gem summary}
s.description = %q{TODO: Write a gem description}
s.rubyforge_project = "gaucho"
-
+ s.add_development_dependency "rspec", "~> 2.6"
+
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
View
136 spec/fixtures_helper.rb
@@ -0,0 +1,136 @@
+# encoding: utf-8
+
+require 'pp'
+require 'fileutils'
+require 'yaml'
+
+class Repo
+ # create a new repo
+ def initialize(repo_path)
+ @time = Time.new(1999, 12, 31, 12, 0, 0, '-05:00')
+ @repo_path = File.join 'fixtures', repo_path
+ @pwd = FileUtils.pwd
+ FileUtils.rm_rf @repo_path
+ FileUtils.mkdir_p @repo_path
+ FileUtils.cd @repo_path
+ `git init .`
+ yield self if block_given?
+ done
+ end
+
+ # cleanup
+ def done
+ FileUtils.cd @pwd
+ end
+
+ # write some files
+ def write(files = {}, mode = 'w')
+ files.each do |path, content|
+ FileUtils.mkdir_p(File.dirname path)
+ File.open(path, mode) {|f| f.write "#{content}\n"}
+ end
+ end
+
+ # append to some files
+ def append(files)
+ write files, 'a'
+ end
+
+ # git add some files
+ def git_add(files = ['.'])
+ files.each {|file| `git add #{file}`}
+ end
+
+ # git commit with message
+ def git_commit(message = 'bloops')
+ # fake author / commit dates
+ @time += 86400
+ author_time = "#{@time.to_i} -0500"
+ @time += 86400
+ commit_time = "#{@time.to_i} -0500"
+ `export GIT_AUTHOR_DATE="#{author_time}"; export GIT_COMMITTER_DATE="#{commit_time}"; git commit -m "#{message}"`
+ end
+end
+
+class Pageset < Repo
+ def page(page_name, &block)
+ Page.new self, page_name, &block
+ end
+end
+
+class Page
+ def initialize(pageset, page_name)
+ @pageset = pageset
+ @page_name = page_name
+ yield self if block_given?
+ end
+
+ # page index file
+ def index_path
+ File.join(@page_name, 'index.md')
+ end
+
+ # get/set metadata
+ def meta(meta = nil)
+ docs = read_index
+ if !meta.nil?
+ docs[0].merge! meta
+ write_index docs
+ end
+ docs[0]
+ end
+
+ # get/set content
+ def content(content = nil)
+ docs = read_index
+ if !content.nil?
+ docs[1] = content
+ write_index docs
+ end
+ docs[1]
+ end
+
+ # get all yaml docs from this page's index.md file
+ def read_index
+ docs = []
+ if File.exists? index_path
+ File.open(index_path) {|file| YAML.each_document(file) {|doc| docs << doc}}
+ end
+ docs[0] = {} if docs[0].nil?
+ docs[1] = '' if docs[1].nil?
+ docs
+ end
+
+ # write all yaml docs to this page's index.md file
+ def write_index(docs)
+ # Since this script tries to simulate how user data will actually be stored,
+ # ie. without extra quoting or \\u-style escaping, and because .to_yaml escapes
+ # unicode and quotes multi-line strings, YAML serialization is done manually.
+ metas = []
+ docs[0].each do |key, value|
+ metas << if value.instance_of? Array
+ "#{key}: [#{value.join(', ')}]"
+ else
+ "#{key}: #{value}"
+ end
+ end
+ meta = if metas.empty?
+ '---'
+ else
+ metas.join("\n")
+ end
+ @pageset.write index_path => "#{meta}\n--- |\n#{docs[1]}\n"
+ end
+
+ # write some files
+ def write(files = {}, mode = 'w')
+ f = {}
+ files.each {|path, content| f[File.join @page_name, path] = content}
+ @pageset.write f, mode
+ end
+
+ # append to some files
+ def append(files)
+ write files, 'a'
+ end
+end
View
9 spec/gaucho/pageset_spec.rb
@@ -0,0 +1,9 @@
+require 'gaucho/pageset'
+
+module Gaucho
+ describe Pageset do
+ it "" do
+ # something
+ end
+ end
+end
View
43 spec/generate_fixtures.rb
@@ -0,0 +1,43 @@
+# encoding: utf-8
+
+require './fixtures_helper'
+
+Pageset.new 'test' do |r|
+ r.write 'foo/bar.txt' => 'this is some sample content for foo'
+ r.write 'baz.txt' => 'this is some sample content for bar'
+ r.git_add
+ r.git_commit 'commit 1'
+
+ r.append 'baz.txt' => 'bar has been modified!!!'
+ r.git_add
+ r.git_commit 'commit 2'
+end
+
+Pageset.new 'two-pages' do |r|
+ r.page 'page-one' do |p|
+ p.meta 'Title' => 'Page One!', 'Tags' => %w{foo bar}
+ p.content 'this is sample page one'
+ r.git_add
+ r.git_commit 'page 1, commit 1'
+
+ p.meta 'Title' => 'Page One Modified!', 'Tags' => %w{foo bar baz}
+ r.git_add
+ r.git_commit 'page 1, commit 2'
+ end
+
+ r.page 'page-two' do |p|
+ p.meta 'Title' => 'Page Two!', 'Tags' => %w{bar baz}
+ p.content 'this is sample page two'
+ r.git_add
+ r.git_commit 'page 2, commit 1'
+
+ p.content 'this is sample page two, modified!'
+ p.write 'foo.txt' => 'content for foo.txt'
+ r.git_add
+ r.git_commit 'page 2, commit 2'
+
+ p.append 'foo.txt' => 'more content for foo.txt!'
+ r.git_add
+ r.git_commit 'page 2, commit 3'
+ end
+end
View
0 spec/spec_helper.rb
No changes.

0 comments on commit 34d0788

Please sign in to comment.