diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-01-23 15:45:34 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-01-28 12:29:34 +0100 |
commit | d28e5b94a7ef90cd9e569f5c12485814fcadd70a (patch) | |
tree | c483c37f26a98e4bad4671869067bf6f07216cc6 /activesupport/lib/active_support/testing/file_fixtures.rb | |
parent | 71a84206ab4d3488ac0d522a7375efc67301aae5 (diff) | |
download | rails-d28e5b94a7ef90cd9e569f5c12485814fcadd70a.tar.gz rails-d28e5b94a7ef90cd9e569f5c12485814fcadd70a.tar.bz2 rails-d28e5b94a7ef90cd9e569f5c12485814fcadd70a.zip |
introduce `ActiveSupport::Testing::FileFixtures`.
It's a thin layer to provide easy access to sample files throughout
test-cases. This adds the directory `test/fixtures/files` to newly
generated applications.
Diffstat (limited to 'activesupport/lib/active_support/testing/file_fixtures.rb')
-rw-r--r-- | activesupport/lib/active_support/testing/file_fixtures.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/testing/file_fixtures.rb b/activesupport/lib/active_support/testing/file_fixtures.rb new file mode 100644 index 0000000000..4c6a0801b8 --- /dev/null +++ b/activesupport/lib/active_support/testing/file_fixtures.rb @@ -0,0 +1,34 @@ +module ActiveSupport + module Testing + # Adds simple access to sample files called file fixtures. + # File fixtures are normal files stored in + # <tt>ActiveSupport::TestCase.file_fixture_path</tt>. + # + # File fixtures are represented as +Pathname+ objects. + # This makes it easy to extract specific information: + # + # file_fixture("example.txt").read # get the file's content + # file_fixture("example.mp3").size # get the file size + module FileFixtures + extend ActiveSupport::Concern + + included do + class_attribute :file_fixture_path, instance_writer: false + end + + # Returns a +Pathname+ to the fixture file named +fixture_name+. + # + # Raises ArgumentError if +fixture_name+ can't be found. + def file_fixture(fixture_name) + path = Pathname.new(File.join(file_fixture_path, fixture_name)) + + if path.exist? + path + else + msg = "the directory '%s' does not contain a file named '%s'" + raise ArgumentError, msg % [file_fixture_path, fixture_name] + end + end + end + end +end |