From 13eabe0695f650c4ea20fac05231434f2ec74392 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 3 May 2021 03:42:26 +0530 Subject: [PATCH] added specs --- plugin.rb | 2 + spec/extensions/sprockets_directive_spec.rb | 55 +++++++++++++++++++ .../sprockets/require_tree_discourse_empty.js | 1 + .../require_tree_discourse_non_existant.js | 1 + .../sprockets/require_tree_discourse_test.js | 1 + .../sprockets/resolved_js_file_contents.txt | 3 + 6 files changed, 63 insertions(+) create mode 100644 spec/extensions/sprockets_directive_spec.rb create mode 100644 spec/fixtures/sprockets/require_tree_discourse_empty.js create mode 100644 spec/fixtures/sprockets/require_tree_discourse_non_existant.js create mode 100644 spec/fixtures/sprockets/require_tree_discourse_test.js create mode 100644 spec/fixtures/sprockets/resolved_js_file_contents.txt diff --git a/plugin.rb b/plugin.rb index eaa386b9..46c6fabb 100644 --- a/plugin.rb +++ b/plugin.rb @@ -97,6 +97,8 @@ after_initialize do end add_to_class(::Sprockets::DirectiveProcessor, :process_require_tree_discourse_directive) do |path = "."| + raise ArgumentError, "path cannot be empty" if path == "." + discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" path = File.expand_path(path, discourse_asset_path) stat = @environment.stat(path) diff --git a/spec/extensions/sprockets_directive_spec.rb b/spec/extensions/sprockets_directive_spec.rb new file mode 100644 index 00000000..f5e2df9f --- /dev/null +++ b/spec/extensions/sprockets_directive_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require_relative '../plugin_helper' + +describe "Sprockets: require_tree_discourse directive" do + let(:discourse_asset_path) { + "#{Rails.root}/app/assets/javascripts/" + } + let(:fixture_asset_path) { + "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/" + } + let(:test_file_contents) { + "console.log('hello')" + } + let(:resolved_file_contents) { + File.read( + "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/resolved_js_file_contents.txt" + ) + } + + before do + @env ||= Sprockets::Environment.new + discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" + fixture_asset_path = "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/" + @env.append_path(discourse_asset_path) + @env.append_path(fixture_asset_path) + @env.cache = {} + end + + def create_tmp_folder_and_run(path, file_contents, &block) + dir = File.dirname(path) + unless File.directory?(dir) + FileUtils.mkdir_p(dir) + end + + File.new(path, 'w') + File.write(path, file_contents) + yield block if block_given? + File.delete(path) + end + + it "includes assets from the discourse core" do + create_tmp_folder_and_run("#{discourse_asset_path}/sptest/test.js", test_file_contents) do + expect(@env.find_asset("require_tree_discourse_test.js").to_s).to eq(resolved_file_contents) + end + end + + it "throws ArgumentError if path is empty" do + expect { @env.find_asset("require_tree_discourse_empty.js") }.to raise_error(ArgumentError).with_message("path cannot be empty") + end + + it "throws ArgumentError if path is non non-existent" do + expect { @env.find_asset("require_tree_discourse_non_existant.js") }.to raise_error(ArgumentError) + end +end diff --git a/spec/fixtures/sprockets/require_tree_discourse_empty.js b/spec/fixtures/sprockets/require_tree_discourse_empty.js new file mode 100644 index 00000000..df264ec5 --- /dev/null +++ b/spec/fixtures/sprockets/require_tree_discourse_empty.js @@ -0,0 +1 @@ +//= require_tree_discourse \ No newline at end of file diff --git a/spec/fixtures/sprockets/require_tree_discourse_non_existant.js b/spec/fixtures/sprockets/require_tree_discourse_non_existant.js new file mode 100644 index 00000000..d9b2be76 --- /dev/null +++ b/spec/fixtures/sprockets/require_tree_discourse_non_existant.js @@ -0,0 +1 @@ +//= require_tree_discourse dummy_path \ No newline at end of file diff --git a/spec/fixtures/sprockets/require_tree_discourse_test.js b/spec/fixtures/sprockets/require_tree_discourse_test.js new file mode 100644 index 00000000..a86aa0d7 --- /dev/null +++ b/spec/fixtures/sprockets/require_tree_discourse_test.js @@ -0,0 +1 @@ +//= require_tree_discourse sptest \ No newline at end of file diff --git a/spec/fixtures/sprockets/resolved_js_file_contents.txt b/spec/fixtures/sprockets/resolved_js_file_contents.txt new file mode 100644 index 00000000..53e2cfa2 --- /dev/null +++ b/spec/fixtures/sprockets/resolved_js_file_contents.txt @@ -0,0 +1,3 @@ +eval("define(\"sptest/test\", [], function () {\n \"use strict\";\n\n console.log('hello');\n});" + "\n//# sourceURL=sptest/test"); +; +eval("" + "\n//# sourceURL=require_tree_discourse_test");