Created
February 23, 2013 15:48
-
-
Save raws/5020196 to your computer and use it in GitHub Desktop.
Recursive file system stubs for fakefs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'fileutils' | |
| require 'yaml' | |
| module Tower | |
| module FixtureHelpers | |
| def self.fixture(fixture_name) | |
| fixtures[fixture_name] || fixture_not_found!(fixture_name) | |
| end | |
| def self.fixture_not_found!(fixture_name) | |
| raise "No fixture #{fixture_name.inspect} in #{fixtures.keys.inspect}" | |
| end | |
| def self.fixtures | |
| @fixtures ||= {} | |
| end | |
| def self.preload_fixtures | |
| fixtures_path = File.join(File.dirname(__FILE__), '..', 'fixtures', '*.yml') | |
| Dir.glob(fixtures_path).each do |fixture_path| | |
| fixture_key = File.basename(fixture_path).slice(/\A(.*)\./, 1).to_sym | |
| fixtures[fixture_key] = YAML.load_file(fixture_path) | |
| end | |
| end | |
| def stub_file_structure_from_fixture(fixture_name) | |
| FixtureHelpers.fixture(fixture_name).tap do |nodes| | |
| stub_tree '/', nodes | |
| end | |
| end | |
| private | |
| def create_file(path) | |
| FileUtils.mkpath File.dirname(path) | |
| FileUtils.touch path | |
| end | |
| def stub_tree(prefix, nodes) | |
| nodes.each do |node| | |
| if node.is_a?(String) | |
| create_file File.join(prefix, node) | |
| elsif node.is_a?(Hash) | |
| node.each do |directory, contents| | |
| stub_tree File.join(prefix, directory), contents | |
| end | |
| else | |
| raise "#{node.class.name} is not an accepted fixture node type: #{node.inspect}" | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment