Skip to content

Commit

Permalink
Fix RSpec/DescribedClass.
Browse files Browse the repository at this point in the history
This error appears something like ``RSpec/DescribedClass: Use `described_class` instead of `PDK::Util`.``.  This rule is triggered when the class under test is referenced directly instead of using `described_class`.

Signed-off-by: Gavin Didrichsen <[email protected]>
  • Loading branch information
gavindidrichsen committed Mar 1, 2024
1 parent 3d36b80 commit 7fa0468
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 72 deletions.
20 changes: 10 additions & 10 deletions spec/unit/pdk/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ def mock_file(path, content)

describe '.system_config' do
it 'returns a PDK::Config::Namespace' do
expect(config.system_config).to be_a(PDK::Config::Namespace)
expect(config.system_config).to be_a(described_class::Namespace)
end
end

describe '.user_config' do
it 'returns a PDK::Config::Namespace' do
expect(config.user_config).to be_a(PDK::Config::Namespace)
expect(config.user_config).to be_a(described_class::Namespace)
end
end

describe '.project_config' do
it 'returns a PDK::Config::Namespace' do
expect(config.project_config).to be_a(PDK::Config::Namespace)
expect(config.project_config).to be_a(described_class::Namespace)
end

context 'Given a Control Repo context' do
Expand Down Expand Up @@ -115,7 +115,7 @@ def mock_file(path, content)
end

it 'traverses root names' do
expect(config.get('user')).to be_a(PDK::Config::Namespace)
expect(config.get('user')).to be_a(described_class::Namespace)
end

it 'traverses namespaces' do
Expand Down Expand Up @@ -565,10 +565,10 @@ def project_config

context 'and the bolt configuration is unparsable' do
before do
allow(PDK::Config::YAML).to receive(:new).and_call_original
allow(PDK::Config::YAML).to receive(:new)
allow(described_class::YAML).to receive(:new).and_call_original
allow(described_class::YAML).to receive(:new)
.with(file: PDK::Util::Filesystem.expand_path(bolt_analytics_path))
.and_raise(PDK::Config::LoadError)
.and_raise(described_class::LoadError)
end

it 'returns true' do
Expand Down Expand Up @@ -633,10 +633,10 @@ def uuid_regex(uuid)

context 'and the bolt configuration is unparsable' do
before do
allow(PDK::Config::YAML).to receive(:new).and_call_original
allow(PDK::Config::YAML).to receive(:new)
allow(described_class::YAML).to receive(:new).and_call_original
allow(described_class::YAML).to receive(:new)
.with(file: PDK::Util::Filesystem.expand_path(bolt_analytics_path))
.and_raise(PDK::Config::LoadError)
.and_raise(described_class::LoadError)
end

it 'generates a new UUID' do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/pdk/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
it 'stores the events in the report by source' do
expect(report).to have_attributes(
events: {
'puppet-lint' => [instance_of(PDK::Report::Event)],
'rubocop' => [instance_of(PDK::Report::Event)]
'puppet-lint' => [instance_of(described_class::Event)],
'rubocop' => [instance_of(described_class::Event)]
}
)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/pdk/template/fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
let(:template_uri) { PDK::Util::TemplateURI.new('https://github.com/puppetlabs/pdk-templates') }

it 'creates a Git Fetcher object' do
expect(instance).to be_a(PDK::Template::Fetcher::Git)
expect(instance).to be_a(described_class::Git)
end
end

context 'given any other uri' do
let(:template_uri) { PDK::Util::TemplateURI.new('/some/path') }

before do
allow(PDK::Template::Fetcher::Git).to receive(:fetchable?).and_return(false)
allow(described_class::Git).to receive(:fetchable?).and_return(false)
end

it 'creates a Local Fetcher object' do
expect(instance).to be_a(PDK::Template::Fetcher::Local)
expect(instance).to be_a(described_class::Local)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/template/renderer/v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

describe '.instance' do
it 'creates a PDK::Template::Renderer::V1::Renderer object' do
expect(described_class.instance(template_root, template_uri, pdk_context)).to be_a(PDK::Template::Renderer::V1::Renderer)
expect(described_class.instance(template_root, template_uri, pdk_context)).to be_a(described_class::Renderer)
end
end
end
6 changes: 3 additions & 3 deletions spec/unit/pdk/template/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

context 'given an original template directory' do
before do
allow(PDK::Template::Renderer::V1).to receive(:compatible?).and_return(true)
allow(described_class::V1).to receive(:compatible?).and_return(true)
end

it 'creates a version 1 renderer' do
expect(instance).to be_a(PDK::Template::Renderer::V1::Renderer)
expect(instance).to be_a(described_class::V1::Renderer)
end
end

context 'given a template that has no appropriate renderer' do
before do
allow(PDK::Template::Renderer::V1).to receive(:compatible?).and_return(false)
allow(described_class::V1).to receive(:compatible?).and_return(false)
end

it 'creates a Local Fetcher object' do
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/pdk/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
end

context 'when initialized correctly' do
let(:fetcher) { PDK::Template::Fetcher::AbstractFetcher.new(template_uri, {}) }
let(:fetcher) { described_class::Fetcher::AbstractFetcher.new(template_uri, {}) }
let(:template_dir) do
PDK::Template::TemplateDir.new(
described_class::TemplateDir.new(
template_uri,
nil,
pdk_context,
instance_double(PDK::Template::Renderer::AbstractRenderer)
instance_double(described_class::Renderer::AbstractRenderer)
)
end

before do
expect(PDK::Template::Fetcher).to receive(:with).with(template_uri).and_yield(fetcher)
allow(PDK::Template::TemplateDir).to receive(:instance).with(template_uri, anything, pdk_context).and_return(template_dir)
expect(described_class::Fetcher).to receive(:with).with(template_uri).and_yield(fetcher)
allow(described_class::TemplateDir).to receive(:instance).with(template_uri, anything, pdk_context).and_return(template_dir)
end

it 'fetches remote templates' do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/pdk/util/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
let(:gemfile) { '/Gemfile' }
let(:gemfile_lock) { "#{gemfile}.lock" }
let(:bundle_helper) do
instance_double(PDK::Util::Bundler::BundleHelper, gemfile: gemfile, gemfile?: true, gemfile_lock: gemfile_lock)
instance_double(described_class::BundleHelper, gemfile: gemfile, gemfile?: true, gemfile_lock: gemfile_lock)
end

before do
# Allow us to mock/stub/expect calls to the internal bundle helper.
allow(PDK::Util::Bundler::BundleHelper).to receive(:new).and_return(bundle_helper)
allow(described_class::BundleHelper).to receive(:new).and_return(bundle_helper)
allow(PDK::Util::Filesystem).to receive(:mv).with(gemfile_lock, anything)
allow(PDK::Util::Filesystem).to receive(:mv).with(anything, gemfile_lock, force: true)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/util/vendored_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
allow(mock_http).to receive(:verify_mode=).with(anything)
end

let(:download_error) { PDK::Util::VendoredFile::DownloadError }
let(:download_error) { described_class::DownloadError }
let(:mock_http) { instance_double(Net::HTTP) }
let(:mock_request) { instance_double(Net::HTTP::Get) }
let(:mock_response) { instance_double(Net::HTTPResponse) }
Expand Down
82 changes: 41 additions & 41 deletions spec/unit/pdk/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
let(:version_file) { File.join('path', 'to', 'the', 'version', 'file') }

before do
allow(PDK::Util::Version).to receive(:version_file).and_return(version_file)
allow(described_class::Version).to receive(:version_file).and_return(version_file)
end
end

shared_context 'without version file', version_file: false do
before do
allow(PDK::Util::Version).to receive(:version_file).and_return(nil)
allow(described_class::Version).to receive(:version_file).and_return(nil)
end
end

Expand All @@ -38,24 +38,24 @@
let(:actual_start_dir) { '/path/to/something/deep/in/a/module' }

before do
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path/to/something/deep/in/a/module').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep/in/a/module').and_return('/path/to/something/deep/in/a')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path/to/something/deep/in/a').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep/in/a').and_return('/path/to/something/deep/in')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path/to/something/deep/in').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep/in').and_return('/path/to/something/deep')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path/to/something/deep').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep').and_return('/path/to/something')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path/to/something').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path/to/something').and_return('/path/to')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path/to').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path/to').and_return('/path')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/path').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/path').and_return('/')
allow(PDK::Util::Filesystem).to receive(:directory?).with('/').and_return(true)
allow(PDK::Util::Filesystem).to receive(:expand_path).with('..', '/').and_return('/')
allow(PDK::Util::Filesystem).to receive(:expand_path).with(actual_start_dir).and_return(actual_start_dir)
allow(PDK::Util::Filesystem).to receive(:file?).with(a_string_matching(/metadata\.json\Z/)).and_return(false)
allow(described_class::Filesystem).to receive(:directory?).with('/path/to/something/deep/in/a/module').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep/in/a/module').and_return('/path/to/something/deep/in/a')
allow(described_class::Filesystem).to receive(:directory?).with('/path/to/something/deep/in/a').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep/in/a').and_return('/path/to/something/deep/in')
allow(described_class::Filesystem).to receive(:directory?).with('/path/to/something/deep/in').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep/in').and_return('/path/to/something/deep')
allow(described_class::Filesystem).to receive(:directory?).with('/path/to/something/deep').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path/to/something/deep').and_return('/path/to/something')
allow(described_class::Filesystem).to receive(:directory?).with('/path/to/something').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path/to/something').and_return('/path/to')
allow(described_class::Filesystem).to receive(:directory?).with('/path/to').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path/to').and_return('/path')
allow(described_class::Filesystem).to receive(:directory?).with('/path').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/path').and_return('/')
allow(described_class::Filesystem).to receive(:directory?).with('/').and_return(true)
allow(described_class::Filesystem).to receive(:expand_path).with('..', '/').and_return('/')
allow(described_class::Filesystem).to receive(:expand_path).with(actual_start_dir).and_return(actual_start_dir)
allow(described_class::Filesystem).to receive(:file?).with(a_string_matching(/metadata\.json\Z/)).and_return(false)
end

context 'when start_dir is nil' do
Expand All @@ -65,7 +65,7 @@

context 'and the target file exists' do
before do
allow(PDK::Util::Filesystem).to receive(:file?).with('/path/to/something/metadata.json').and_return(true)
allow(described_class::Filesystem).to receive(:file?).with('/path/to/something/metadata.json').and_return(true)
end

it { is_expected.to eq('/path/to/something/metadata.json') }
Expand All @@ -81,7 +81,7 @@

context 'and the target file exists' do
before do
allow(PDK::Util::Filesystem).to receive(:file?).with('/path/to/something/metadata.json').and_return(true)
allow(described_class::Filesystem).to receive(:file?).with('/path/to/something/metadata.json').and_return(true)
end

it { is_expected.to eq('/path/to/something/metadata.json') }
Expand Down Expand Up @@ -111,7 +111,7 @@

context 'and the path does not exist' do
before do
allow(PDK::Util::Filesystem).to receive(:exist?).with(path).and_return(false)
allow(described_class::Filesystem).to receive(:exist?).with(path).and_return(false)
end

it 'raises a FatalError' do
Expand All @@ -123,11 +123,11 @@

context 'and the path exists' do
before do
allow(PDK::Util::Filesystem).to receive(:exist?).with(path).and_return(true)
allow(described_class::Filesystem).to receive(:exist?).with(path).and_return(true)
end

it 'calls Puppet::Util::Windows::File.get_long_pathname to resolve the absolute path' do
expect(PDK::Util::Windows::File).to receive(:get_long_pathname).with(path)
expect(described_class::Windows::File).to receive(:get_long_pathname).with(path)

described_class.canonical_path(path)
end
Expand All @@ -140,7 +140,7 @@
end

it 'calls File.expath_path to resolve the absolute path' do
expect(PDK::Util::Filesystem).to receive(:expand_path).with(path)
expect(described_class::Filesystem).to receive(:expand_path).with(path)

described_class.canonical_path(path)
end
Expand All @@ -164,7 +164,7 @@

context 'when running from a release' do
before do
allow(PDK::Util::Version).to receive(:git_ref).and_return(nil)
allow(described_class::Version).to receive(:git_ref).and_return(nil)
stub_const('PDK::VERSION', '1.3.0')
end

Expand All @@ -173,7 +173,7 @@

context 'when running from a pre-release' do
before do
allow(PDK::Util::Version).to receive(:git_ref).and_return(nil)
allow(described_class::Version).to receive(:git_ref).and_return(nil)
stub_const('PDK::VERSION', '1.3.0.pre')
end

Expand All @@ -182,7 +182,7 @@

context 'when running from git' do
before do
allow(PDK::Util::Version).to receive(:git_ref).and_return('abc')
allow(described_class::Version).to receive(:git_ref).and_return('abc')
end

it { is_expected.to be true }
Expand Down Expand Up @@ -255,7 +255,7 @@
context 'when running on Windows' do
before do
allow(Gem).to receive(:win_platform?).and_return(true)
allow(PDK::Util::Env).to receive(:[]).with('LOCALAPPDATA').and_return('C:/Users/test')
allow(described_class::Env).to receive(:[]).with('LOCALAPPDATA').and_return('C:/Users/test')
end

it 'returns a path in the %LOCALAPPDATA% folder' do
Expand All @@ -281,7 +281,7 @@
context 'when running on Windows' do
before do
allow(Gem).to receive(:win_platform?).and_return(true)
allow(PDK::Util::Env).to receive(:[]).with('LOCALAPPDATA').and_return('C:/Users/test')
allow(described_class::Env).to receive(:[]).with('LOCALAPPDATA').and_return('C:/Users/test')
end

it 'returns a path in the %LOCALAPPDATA% folder' do
Expand All @@ -293,7 +293,7 @@
before do
allow(Gem).to receive(:win_platform?).and_return(false)
allow(Dir).to receive(:home).with(any_args).and_return('/home/test')
allow(PDK::Util::Env).to receive(:fetch)
allow(described_class::Env).to receive(:fetch)
.with('XDG_CONFIG_HOME', '/home/test/.config')
.and_return('/xdg_home/test/.config')
end
Expand Down Expand Up @@ -323,8 +323,8 @@
context 'when ProgramData environment variable exists' do
before do
# ProgramData was added in Windows Vista
allow(PDK::Util::Env).to receive(:[]).with('ProgramData').and_return(program_data)
allow(PDK::Util::Env).to receive(:[]).with('AllUsersProfile').and_return(nil)
allow(described_class::Env).to receive(:[]).with('ProgramData').and_return(program_data)
allow(described_class::Env).to receive(:[]).with('AllUsersProfile').and_return(nil)
end

it 'returns a path in the Program Data directory' do
Expand All @@ -335,8 +335,8 @@
context 'when AllUsersProfile environment variable exists' do
before do
# AllUsersProfile was added in Windows 2000
allow(PDK::Util::Env).to receive(:[]).with('ProgramData').and_return(nil)
allow(PDK::Util::Env).to receive(:[]).with('AllUsersProfile').and_return(program_data)
allow(described_class::Env).to receive(:[]).with('ProgramData').and_return(nil)
allow(described_class::Env).to receive(:[]).with('AllUsersProfile').and_return(program_data)
end

it 'returns a path in the Program Data directory' do
Expand Down Expand Up @@ -423,32 +423,32 @@
let(:test_path) { '\x00path/test' }

before do
allow(PDK::Util::Filesystem).to receive(:directory?).and_call_original
allow(described_class::Filesystem).to receive(:directory?).and_call_original
end

# Directories which indicate a module root
['manifests', 'lib/puppet', 'lib/puppet_x', 'lib/facter', 'tasks', 'facts.d', 'functions', 'types'].each do |testcase|
it "detects #{testcase} as being in the root of a module" do
allow(PDK::Util::Filesystem).to receive(:directory?).with(File.join(test_path, testcase)).and_return(true)
allow(described_class::Filesystem).to receive(:directory?).with(File.join(test_path, testcase)).and_return(true)
expect(described_class.in_module_root?(test_path)).to be(true)
end
end

# Directories which do not indicate a module root
['lib', 'Boltdir', 'puppet'].each do |testcase|
it "detects #{testcase} as not being in the root of a module" do
allow(PDK::Util::Filesystem).to receive(:directory?).with(File.join(test_path, testcase)).and_return(true)
allow(described_class::Filesystem).to receive(:directory?).with(File.join(test_path, testcase)).and_return(true)
expect(described_class.in_module_root?(test_path)).to be(false)
end
end

it 'detects metadata.json within the folder and determines that it is the root of a module' do
allow(PDK::Util::Filesystem).to receive(:file?).with(File.join(test_path, 'metadata.json')).and_return(true)
allow(described_class::Filesystem).to receive(:file?).with(File.join(test_path, 'metadata.json')).and_return(true)
expect(described_class.in_module_root?(test_path)).to be(true)
end

it 'uses the current directory if a directory is not specified' do
expect(PDK::Util::Filesystem).to receive(:directory?) { |path| expect(path).to start_with(Dir.pwd) }.at_least(:once)
expect(described_class::Filesystem).to receive(:directory?) { |path| expect(path).to start_with(Dir.pwd) }.at_least(:once)
described_class.in_module_root?
end
end
Expand Down
Loading

0 comments on commit 7fa0468

Please sign in to comment.