require 'spec_helper' describe Middleman::Cli::CDN do let(:subject) { described_class.new } describe '.exit_on_failure?' do it "should return true" do expect(described_class.exit_on_failure?).to eq(true) end end describe '.say_status' do context "defaults" do it "should say" do described_class.say_status(nil, "a status") end end end describe '#cdn_invalidate' do before do allow(Dir).to receive(:chdir).with(anything) { |path, &block| block.call } allow(Dir).to receive(:glob).with('**/*', File::FNM_DOTMATCH).and_return([".", "index.html", "image.png"]) allow(File).to receive(:directory?).with(".").and_return(true) allow(File).to receive(:directory?).with("index.html").and_return(false) allow(File).to receive(:directory?).with("image.png").and_return(false) end context "all files matched" do context "no cdn provided" do let(:options) do OpenStruct.new({ filter: /.*/ }) end it "should invalidate the files with only cloudflare" do expect_any_instance_of(::Middleman::Cli::CloudFlareCDN).to_not receive(:invalidate) expect_any_instance_of(::Middleman::Cli::CloudFrontCDN).to_not receive(:invalidate) expect { subject.cdn_invalidate(options) }.to raise_error(RuntimeError) end end context "one cdn provided" do let(:options) do OpenStruct.new({ cloudflare: {}, filter: /.*/ }) end it "should invalidate the files with only cloudflare" do expect_any_instance_of(::Middleman::Cli::CloudFlareCDN).to receive(:invalidate).with(options.cloudflare, ["/index.html", "/image.png", "/"]) expect_any_instance_of(::Middleman::Cli::CloudFrontCDN).to_not receive(:invalidate) subject.cdn_invalidate(options) end end context "all cdns provided" do let(:options) do OpenStruct.new({ cloudflare: {}, cloudfront: {}, filter: /.*/ }) end it "should invalidate the files with all cdns" do expect_any_instance_of(::Middleman::Cli::CloudFlareCDN).to receive(:invalidate).with(options.cloudflare, ["/index.html", "/image.png", "/"]) expect_any_instance_of(::Middleman::Cli::CloudFrontCDN).to receive(:invalidate).with(options.cloudfront, ["/index.html", "/image.png", "/"]) subject.cdn_invalidate(options) end end end context "some files matched" do let(:options) do OpenStruct.new({ cloudflare: {}, cloudfront: {}, filter: /\.html/ }) end it "should invalidate the files with all cdns" do expect_any_instance_of(::Middleman::Cli::CloudFlareCDN).to receive(:invalidate).with(options.cloudflare, ["/index.html", "/"]) expect_any_instance_of(::Middleman::Cli::CloudFrontCDN).to receive(:invalidate).with(options.cloudfront, ["/index.html", "/"]) subject.cdn_invalidate(options) end end context "no files matched" do let(:options) do OpenStruct.new({ cloudflare: {}, cloudfront: {}, filter: /\.htm$/ }) end it "should invalidate the files with all cdns" do expect_any_instance_of(::Middleman::Cli::CloudFlareCDN).to_not receive(:invalidate) expect_any_instance_of(::Middleman::Cli::CloudFrontCDN).to_not receive(:invalidate) subject.cdn_invalidate(options) end end end end