From 987de8d0b5b5bf17710e24cc68993e48c5fdae68 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Mon, 7 Apr 2025 16:14:24 +0530 Subject: [PATCH 1/7] trying to add ruby 3.4 pipeline in chef-cli Signed-off-by: nikhil2611 --- .expeditor/verify.pipeline.yml | 34 +++++++++++++++++++ Gemfile | 2 ++ .../cookbook_location_specification_spec.rb | 2 +- spec/unit/policyfile_demands_spec.rb | 6 ++-- spec/unit/policyfile_evaluation_spec.rb | 12 +++---- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index 724faebb..9ebc160f 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -27,6 +27,14 @@ steps: docker: image: ruby:3.3 +- label: run-specs-ruby-3.4 + command: + - .expeditor/run_linux_tests.sh rspec + expeditor: + executor: + docker: + image: ruby:3.4 + - label: run-specs-windows-3.1 command: - powershell .expeditor/run_windows_tests.ps1 rspec @@ -46,6 +54,16 @@ steps: shell: ["powershell", "-Command"] image: rubydistros/windows-2019:3.3 +- label: run-specs-windows-ruby-3.4 + command: + - powershell .expeditor/run_windows_tests.ps1 rspec + expeditor: + executor: + docker: + host_os: windows + shell: ["powershell", "-Command"] + image: rubydistros/windows-2019:3.4 + - label: cookstyle-generator-cb-tests-ruby-3.1 command: - .expeditor/run_linux_tests.sh "rake style:cookstyle" @@ -54,6 +72,14 @@ steps: docker: image: ruby:3.1 +- label: cookstyle-generator-cb-tests-ruby-3.4 + command: + - .expeditor/run_linux_tests.sh "rake style:cookstyle" + expeditor: + executor: + docker: + image: ruby:3.4 + - label: chefstyle-tests-ruby-3.1 command: - .expeditor/run_linux_tests.sh "rake style:chefstyle" @@ -61,3 +87,11 @@ steps: executor: docker: image: ruby:3.1 + +- label: chefstyle-tests-ruby-3.4 + command: + - .expeditor/run_linux_tests.sh "rake style:chefstyle" + expeditor: + executor: + docker: + image: ruby:3.4 diff --git a/Gemfile b/Gemfile index d7097f9e..11d7d488 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,8 @@ gemspec gem "logger", "< 1.6" # 1.6 causes errors with mixlib-log < 3.1.1 gem "chefspec" +gem "syslog" + group :test do gem "rake" gem "rspec", "=3.12.0" diff --git a/spec/unit/policyfile/cookbook_location_specification_spec.rb b/spec/unit/policyfile/cookbook_location_specification_spec.rb index 2cf5f8d9..1227ee43 100644 --- a/spec/unit/policyfile/cookbook_location_specification_spec.rb +++ b/spec/unit/policyfile/cookbook_location_specification_spec.rb @@ -182,7 +182,7 @@ expect(cookbook_location_spec).to_not be_valid expect(cookbook_location_spec.errors.size).to eq(1) error = cookbook_location_spec.errors.first - expect(error).to eq("Cookbook `my_cookbook' has invalid source options `{:herp=>\"derp\"}'") + expect(error).to eq("Cookbook `my_cookbook' has invalid source options `{herp: \"derp\"}'") end end diff --git a/spec/unit/policyfile_demands_spec.rb b/spec/unit/policyfile_demands_spec.rb index 81357b87..210e600f 100644 --- a/spec/unit/policyfile_demands_spec.rb +++ b/spec/unit/policyfile_demands_spec.rb @@ -301,7 +301,7 @@ it "emits an error" do message = <<~MESSAGE The installed cookbooks do not contain all the recipes required by your run list(s): - Cookbook 'remote-cb' = 1.1.1 {:artifactserver=>"https://supermarket.example/c/remote-cb/1.1.1/download", :version=>"1.1.1"} + Cookbook 'remote-cb' = 1.1.1 {artifactserver: "https://supermarket.example/c/remote-cb/1.1.1/download", version: "1.1.1"} is missing the following required recipes: * this_recipe_doesnt_exist @@ -332,7 +332,7 @@ it "emits an error" do message = <<~MESSAGE The installed cookbooks do not contain all the recipes required by your run list(s): - Cookbook 'remote-cb' = 1.1.1 {:artifactserver=>"https://supermarket.example/c/remote-cb/1.1.1/download", :version=>"1.1.1"} + Cookbook 'remote-cb' = 1.1.1 {artifactserver: "https://supermarket.example/c/remote-cb/1.1.1/download", version: "1.1.1"} is missing the following required recipes: * this_recipe_doesnt_exist @@ -364,7 +364,7 @@ it "emits an error" do message = <<~MESSAGE The installed cookbooks do not contain all the recipes required by your run list(s): - Cookbook 'remote-cb' = 1.1.1 {:artifactserver=>"https://supermarket.example/c/remote-cb/1.1.1/download", :version=>"1.1.1"} + Cookbook 'remote-cb' = 1.1.1 {artifactserver: "https://supermarket.example/c/remote-cb/1.1.1/download", version: "1.1.1"} is missing the following required recipes: * this_recipe_doesnt_exist * this_also_doesnt_exist diff --git a/spec/unit/policyfile_evaluation_spec.rb b/spec/unit/policyfile_evaluation_spec.rb index a3c09520..3662f4a4 100644 --- a/spec/unit/policyfile_evaluation_spec.rb +++ b/spec/unit/policyfile_evaluation_spec.rb @@ -72,7 +72,7 @@ 1: raise 'oops' Backtrace: - TestPolicyfile.rb:1:in `eval_policyfile' + TestPolicyfile.rb:1:in 'ChefCLI::Policyfile::DSL#eval_policyfile' E expect(policyfile.errors.first).to eq(expected_message) end @@ -213,7 +213,7 @@ it "has an invalid source error" do expect(policyfile.errors.size).to eq(1) - message = "Cookbook `foo' has invalid source options `{:herp=>\"derp\"}'" + message = "Cookbook `foo' has invalid source options `{herp: \"derp\"}'" expect(policyfile.errors.first).to eq(message) end end @@ -546,8 +546,8 @@ expected = <<~EOH Cookbook 'foo' assigned to conflicting sources - Previous source: {:path=>"local_cookbooks/foo"} - Conflicts with: {:chef_server=>"https://mychefserver.example.com"} + Previous source: {path: "local_cookbooks/foo"} + Conflicts with: {chef_server: "https://mychefserver.example.com"} EOH expect(policyfile.errors.size).to eq(1) expect(policyfile.errors.first).to eq(expected) @@ -586,7 +586,7 @@ 2: metadata Backtrace: - TestPolicyfile.rb:2:in `eval_policyfile' + TestPolicyfile.rb:2:in 'ChefCLI::Policyfile::DSL#eval_policyfile' EOH expect(File).to receive(:exist?).with("./metadata.rb").and_return(false) expect(File).to receive(:exist?).with("./metadata.json").and_return(false) @@ -612,7 +612,7 @@ 2: metadata Backtrace: - TestPolicyfile.rb:2:in `eval_policyfile' + TestPolicyfile.rb:2:in 'ChefCLI::Policyfile::DSL#eval_policyfile' EOH expect(File).to receive(:exist?).with("./metadata.rb").and_return(true) expect(File).to receive(:exist?).with("./metadata.json").and_return(true) From 6cf6dd07e8b5d2ccde747edc1bbe50ae6932b2ce Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Apr 2025 14:03:54 +0530 Subject: [PATCH 2/7] updating code to work with ruby 3.1 and 3.4 Signed-off-by: nikhil2611 --- .../policyfile/cookbook_location_specification.rb | 3 ++- lib/chef-cli/policyfile/dsl.rb | 10 ++++++---- lib/chef-cli/policyfile_compiler.rb | 9 ++++++--- .../cookbook_location_specification_spec.rb | 2 +- spec/unit/policyfile_demands_spec.rb | 8 +++++--- spec/unit/policyfile_evaluation_spec.rb | 12 ++++++------ 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/chef-cli/policyfile/cookbook_location_specification.rb b/lib/chef-cli/policyfile/cookbook_location_specification.rb index 979b4dab..379b37c1 100644 --- a/lib/chef-cli/policyfile/cookbook_location_specification.rb +++ b/lib/chef-cli/policyfile/cookbook_location_specification.rb @@ -89,7 +89,8 @@ def valid? def errors error_messages = [] if source_options_invalid? - error_messages << "Cookbook `#{name}' has invalid source options `#{source_options.inspect}'" + formatted_opts = source_options.inspect.gsub(/(\w+): /, ':\1=>') + error_messages << "Cookbook `#{name}' has invalid source options `#{formatted_opts}'" end error_messages end diff --git a/lib/chef-cli/policyfile/dsl.rb b/lib/chef-cli/policyfile/dsl.rb index 9108b5e3..a63b39d6 100644 --- a/lib/chef-cli/policyfile/dsl.rb +++ b/lib/chef-cli/policyfile/dsl.rb @@ -110,14 +110,13 @@ def cookbook(name, *version_and_source_opts) else {} end - constraint = version_and_source_opts.first || ">= 0.0.0" spec = CookbookLocationSpecification.new(name, constraint, source_options, storage_config) if ( existing_source = @cookbook_location_specs[name] ) err = "Cookbook '#{name}' assigned to conflicting sources\n\n" - err << "Previous source: #{existing_source.source_options.inspect}\n" - err << "Conflicts with: #{source_options.inspect}\n" + err << "Previous source: {" + existing_source.source_options.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + "}\n" + err << "Conflicts with: {" + source_options.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + "}\n" @errors << err else @cookbook_location_specs[name] = spec @@ -179,8 +178,11 @@ def eval_policyfile(policyfile_string) error_message << " #{error_context(policyfile_string, policyfile_filename, e)}\n\n" unless trace.empty? error_message << " Backtrace:\n" + normalized_trace = trace.map do |line| + line.gsub(/:in ['"]?(?:.*[#.]?)?(eval_policyfile)['"]?/, ":in `\\1'") + end # TODO: need a way to disable filtering - error_message << filtered_bt(policyfile_filename, e).inject("") { |formatted_trace, line| formatted_trace << " #{line}\n" } + error_message << normalized_trace.inject("") { |formatted_trace, line| formatted_trace << " #{line}\n" } end @errors << error_message end diff --git a/lib/chef-cli/policyfile_compiler.rb b/lib/chef-cli/policyfile_compiler.rb index f30847d1..93e41a2c 100644 --- a/lib/chef-cli/policyfile_compiler.rb +++ b/lib/chef-cli/policyfile_compiler.rb @@ -194,13 +194,16 @@ def install unless missing_recipes_by_cb_spec.empty? message = "The installed cookbooks do not contain all the recipes required by your run list(s):\n" missing_recipes_by_cb_spec.each do |spec, missing_items| - message << "#{spec}\nis missing the following required recipes:\n" + formatted_opts = '{' + spec.source_options.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + '}' + message << "Cookbook '#{spec.name}' = #{spec.version} #{formatted_opts}\n" + message << "is missing the following required recipes:\n" + missing_items.each { |_cb, recipe| message << "* #{recipe}\n" } end - + message << "\n" message << "You may have specified an incorrect recipe in your run list,\nor this recipe may not be available in that version of the cookbook\n" - + raise CookbookDoesNotContainRequiredRecipe, message end end diff --git a/spec/unit/policyfile/cookbook_location_specification_spec.rb b/spec/unit/policyfile/cookbook_location_specification_spec.rb index 1227ee43..2cf5f8d9 100644 --- a/spec/unit/policyfile/cookbook_location_specification_spec.rb +++ b/spec/unit/policyfile/cookbook_location_specification_spec.rb @@ -182,7 +182,7 @@ expect(cookbook_location_spec).to_not be_valid expect(cookbook_location_spec.errors.size).to eq(1) error = cookbook_location_spec.errors.first - expect(error).to eq("Cookbook `my_cookbook' has invalid source options `{herp: \"derp\"}'") + expect(error).to eq("Cookbook `my_cookbook' has invalid source options `{:herp=>\"derp\"}'") end end diff --git a/spec/unit/policyfile_demands_spec.rb b/spec/unit/policyfile_demands_spec.rb index 210e600f..d91b4195 100644 --- a/spec/unit/policyfile_demands_spec.rb +++ b/spec/unit/policyfile_demands_spec.rb @@ -209,6 +209,8 @@ instance_double("ChefCLI::Policyfile::CookbookLocationSpecification", name: "remote-cb", + version: "1.1.1", + source_options: remote_cb_source_opts, version_constraint: Semverse::Constraint.new("= 1.1.1"), ensure_cached: nil, to_s: s) @@ -301,7 +303,7 @@ it "emits an error" do message = <<~MESSAGE The installed cookbooks do not contain all the recipes required by your run list(s): - Cookbook 'remote-cb' = 1.1.1 {artifactserver: "https://supermarket.example/c/remote-cb/1.1.1/download", version: "1.1.1"} + Cookbook 'remote-cb' = 1.1.1 {:artifactserver=>"https://supermarket.example/c/remote-cb/1.1.1/download", :version=>"1.1.1"} is missing the following required recipes: * this_recipe_doesnt_exist @@ -332,7 +334,7 @@ it "emits an error" do message = <<~MESSAGE The installed cookbooks do not contain all the recipes required by your run list(s): - Cookbook 'remote-cb' = 1.1.1 {artifactserver: "https://supermarket.example/c/remote-cb/1.1.1/download", version: "1.1.1"} + Cookbook 'remote-cb' = 1.1.1 {:artifactserver=>"https://supermarket.example/c/remote-cb/1.1.1/download", :version=>"1.1.1"} is missing the following required recipes: * this_recipe_doesnt_exist @@ -364,7 +366,7 @@ it "emits an error" do message = <<~MESSAGE The installed cookbooks do not contain all the recipes required by your run list(s): - Cookbook 'remote-cb' = 1.1.1 {artifactserver: "https://supermarket.example/c/remote-cb/1.1.1/download", version: "1.1.1"} + Cookbook 'remote-cb' = 1.1.1 {:artifactserver=>"https://supermarket.example/c/remote-cb/1.1.1/download", :version=>"1.1.1"} is missing the following required recipes: * this_recipe_doesnt_exist * this_also_doesnt_exist diff --git a/spec/unit/policyfile_evaluation_spec.rb b/spec/unit/policyfile_evaluation_spec.rb index 3662f4a4..a3c09520 100644 --- a/spec/unit/policyfile_evaluation_spec.rb +++ b/spec/unit/policyfile_evaluation_spec.rb @@ -72,7 +72,7 @@ 1: raise 'oops' Backtrace: - TestPolicyfile.rb:1:in 'ChefCLI::Policyfile::DSL#eval_policyfile' + TestPolicyfile.rb:1:in `eval_policyfile' E expect(policyfile.errors.first).to eq(expected_message) end @@ -213,7 +213,7 @@ it "has an invalid source error" do expect(policyfile.errors.size).to eq(1) - message = "Cookbook `foo' has invalid source options `{herp: \"derp\"}'" + message = "Cookbook `foo' has invalid source options `{:herp=>\"derp\"}'" expect(policyfile.errors.first).to eq(message) end end @@ -546,8 +546,8 @@ expected = <<~EOH Cookbook 'foo' assigned to conflicting sources - Previous source: {path: "local_cookbooks/foo"} - Conflicts with: {chef_server: "https://mychefserver.example.com"} + Previous source: {:path=>"local_cookbooks/foo"} + Conflicts with: {:chef_server=>"https://mychefserver.example.com"} EOH expect(policyfile.errors.size).to eq(1) expect(policyfile.errors.first).to eq(expected) @@ -586,7 +586,7 @@ 2: metadata Backtrace: - TestPolicyfile.rb:2:in 'ChefCLI::Policyfile::DSL#eval_policyfile' + TestPolicyfile.rb:2:in `eval_policyfile' EOH expect(File).to receive(:exist?).with("./metadata.rb").and_return(false) expect(File).to receive(:exist?).with("./metadata.json").and_return(false) @@ -612,7 +612,7 @@ 2: metadata Backtrace: - TestPolicyfile.rb:2:in 'ChefCLI::Policyfile::DSL#eval_policyfile' + TestPolicyfile.rb:2:in `eval_policyfile' EOH expect(File).to receive(:exist?).with("./metadata.rb").and_return(true) expect(File).to receive(:exist?).with("./metadata.json").and_return(true) From 2de625ac60dcabcc79760875e321b76ac5e80606 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Apr 2025 14:16:13 +0530 Subject: [PATCH 3/7] chefstyle fixes Signed-off-by: nikhil2611 --- lib/chef-cli/policyfile_compiler.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/chef-cli/policyfile_compiler.rb b/lib/chef-cli/policyfile_compiler.rb index 93e41a2c..72811a2a 100644 --- a/lib/chef-cli/policyfile_compiler.rb +++ b/lib/chef-cli/policyfile_compiler.rb @@ -194,16 +194,16 @@ def install unless missing_recipes_by_cb_spec.empty? message = "The installed cookbooks do not contain all the recipes required by your run list(s):\n" missing_recipes_by_cb_spec.each do |spec, missing_items| - formatted_opts = '{' + spec.source_options.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + '}' + formatted_opts = "{" + spec.source_options.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + "}" message << "Cookbook '#{spec.name}' = #{spec.version} #{formatted_opts}\n" message << "is missing the following required recipes:\n" - + missing_items.each { |_cb, recipe| message << "* #{recipe}\n" } end - + message << "\n" message << "You may have specified an incorrect recipe in your run list,\nor this recipe may not be available in that version of the cookbook\n" - + raise CookbookDoesNotContainRequiredRecipe, message end end From 0a23b7212f0732be5e091383fe8aed3432164e74 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Apr 2025 14:26:13 +0530 Subject: [PATCH 4/7] codeQL security fix Signed-off-by: nikhil2611 --- lib/chef-cli/policyfile/cookbook_location_specification.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chef-cli/policyfile/cookbook_location_specification.rb b/lib/chef-cli/policyfile/cookbook_location_specification.rb index 379b37c1..64268bde 100644 --- a/lib/chef-cli/policyfile/cookbook_location_specification.rb +++ b/lib/chef-cli/policyfile/cookbook_location_specification.rb @@ -89,8 +89,8 @@ def valid? def errors error_messages = [] if source_options_invalid? - formatted_opts = source_options.inspect.gsub(/(\w+): /, ':\1=>') - error_messages << "Cookbook `#{name}' has invalid source options `#{formatted_opts}'" + formatted_opts = source_options.map { |k, v| "#{k.inspect}=>#{v.inspect}" }.join(", ") + error_messages << "Cookbook `#{name}' has invalid source options `{#{formatted_opts}}'" end error_messages end From 7c395892836f85d3e09e8492d73d3816b84f942e Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Apr 2025 15:35:08 +0530 Subject: [PATCH 5/7] warning fixes Signed-off-by: nikhil2611 --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index 11d7d488..d6a6d5ca 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ gemspec gem "logger", "< 1.6" # 1.6 causes errors with mixlib-log < 3.1.1 gem "chefspec" gem "syslog" +gem "benchmark" +gem "ostruct" group :test do gem "rake" From f1aed194fec91afd578b8afd1c30706c02464e97 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Apr 2025 20:11:17 +0530 Subject: [PATCH 6/7] removing non required gems Signed-off-by: nikhil2611 --- Gemfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Gemfile b/Gemfile index d6a6d5ca..11d7d488 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,6 @@ gemspec gem "logger", "< 1.6" # 1.6 causes errors with mixlib-log < 3.1.1 gem "chefspec" gem "syslog" -gem "benchmark" -gem "ostruct" group :test do gem "rake" From cb979046cf1f38e585d735d4ee31d0f1a0c68d54 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Mon, 21 Apr 2025 16:20:43 +0530 Subject: [PATCH 7/7] Updated the unit.yml with ruby 3.4 Signed-off-by: nikhil2611 --- .github/workflows/unit.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 61ec18ec..2f4e68d2 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -17,10 +17,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Set up ruby 3.1 + - name: Set up ruby 3.4 uses: ruby/setup-ruby@v1 with: - ruby-version: 3.1 + ruby-version: 3.4 bundler-cache: true - name: run specs run: bundle exec rake spec --trace