How to use save method of Knapsack Package

Best Knapsack_ruby code snippet using Knapsack.save

report_spec.rb

Source:report_spec.rb Github

copy

Full Screen

1describe KnapsackPro::Report do2 describe '.save' do3 subject { described_class.save }4 it do5 test_files = [double]6 tracker = instance_double(KnapsackPro::Tracker, to_a: test_files)7 expect(KnapsackPro).to receive(:tracker).and_return(tracker)8 expect(described_class).to receive(:create_build_subset).with(test_files)9 subject10 end11 end12 describe '.save_subset_queue_to_file' do13 let(:fake_path) { SecureRandom.uuid }14 subject { described_class.save_subset_queue_to_file }15 before do16 test_files = [{path: fake_path}]17 tracker = instance_double(KnapsackPro::Tracker, to_a: test_files)18 expect(KnapsackPro).to receive(:tracker).and_return(tracker)19 subset_queue_id = 'fake-subset-queue-id'20 expect(KnapsackPro::Config::Env).to receive(:subset_queue_id).and_return(subset_queue_id)21 expect(KnapsackPro::Config::TempFiles).to receive(:ensure_temp_directory_exists!)22 queue_id = 'fake-queue-id'23 expect(KnapsackPro::Config::Env).to receive(:queue_id).twice.and_return(queue_id)24 end25 it do26 subject27 expect(28 JSON.parse(29 File.read('.knapsack_pro/queue/fake-queue-id/fake-subset-queue-id.json')30 )31 ).to eq([32 { 'path' => fake_path }33 ])34 end35 end36 describe '.save_node_queue_to_api' do37 context 'when json files with recorded time does exist and test files have measured and default time execution' do38 let(:json_test_file_a_path) { double }39 let(:json_test_file_a) { [{ 'path' => 'a_spec.rb', 'time_execution' => 0.1234 }] }40 let(:json_test_file_b_path) { double }41 let(:json_test_file_b) { [{ 'path' => 'b_spec.rb', 'time_execution' => KnapsackPro::Tracker::DEFAULT_TEST_FILE_TIME }] }42 subject { described_class.save_node_queue_to_api }43 before do44 queue_id = 'fake-queue-id'45 expect(KnapsackPro::Config::Env).to receive(:queue_id).and_return(queue_id)46 expect(Dir).to receive(:glob).with('.knapsack_pro/queue/fake-queue-id/*.json').and_return([47 json_test_file_a_path,48 json_test_file_b_path49 ])50 expect(File).to receive(:read).with(json_test_file_a_path).and_return(json_test_file_a.to_json)51 expect(File).to receive(:read).with(json_test_file_b_path).and_return(json_test_file_b.to_json)52 end53 it 'creates build subset for 2 recorded test files timing' do54 expect(KnapsackPro).not_to receive(:logger)55 expect(described_class).to receive(:create_build_subset).with(56 json_test_file_a + json_test_file_b57 )58 subject59 end60 end61 context 'when json files with recorded time does exist and all test files have default time execution' do62 let(:json_test_file_a_path) { double }63 let(:json_test_file_a) { [{ 'path' => 'a_spec.rb', 'time_execution' => KnapsackPro::Tracker::DEFAULT_TEST_FILE_TIME }] }64 let(:json_test_file_b_path) { double }65 let(:json_test_file_b) { [{ 'path' => 'b_spec.rb', 'time_execution' => KnapsackPro::Tracker::DEFAULT_TEST_FILE_TIME }] }66 subject { described_class.save_node_queue_to_api }67 before do68 queue_id = 'fake-queue-id'69 expect(KnapsackPro::Config::Env).to receive(:queue_id).and_return(queue_id)70 expect(Dir).to receive(:glob).with('.knapsack_pro/queue/fake-queue-id/*.json').and_return([71 json_test_file_a_path,72 json_test_file_b_path73 ])74 expect(File).to receive(:read).with(json_test_file_a_path).and_return(json_test_file_a.to_json)75 expect(File).to receive(:read).with(json_test_file_b_path).and_return(json_test_file_b.to_json)76 end77 it 'logs error on lost info about recorded timing for test files due missing json files AND creates empty build subset' do78 logger = instance_double(Logger)79 expect(KnapsackPro).to receive(:logger).exactly(4).and_return(logger)80 expect(logger).to receive(:warn).with('2 test files were executed on this CI node but the recorded time was lost due to:')81 expect(logger).to receive(:warn).with('1. Please ensure you do not remove the contents of the .knapsack_pro directory between tests run.')82 expect(logger).to receive(:warn).with("2. Ensure you've added Knapsack::Adapters::RSpecAdapter.bind in your rails_helper.rb or spec_helper.rb. Please follow the installation guide again: https://docs.knapsackpro.com/integration/")83 expect(logger).to receive(:warn).with('3. Another potential reason for this warning is that all your tests are empty test files, pending tests, or they have syntax errors, and the time execution was not recorded for them.')84 expect(described_class).to receive(:create_build_subset).with(85 json_test_file_a + json_test_file_b86 )87 subject88 end89 end90 context 'when json files with recorded time does not exist' do91 subject { described_class.save_node_queue_to_api }92 before do93 queue_id = 'fake-queue-id'94 expect(KnapsackPro::Config::Env).to receive(:queue_id).and_return(queue_id)95 expect(Dir).to receive(:glob).with('.knapsack_pro/queue/fake-queue-id/*.json').and_return([])96 end97 it 'logs warning about reasons why no test files were executed on this CI node' do98 logger = instance_double(Logger)99 expect(KnapsackPro).to receive(:logger).exactly(2).and_return(logger)100 expect(logger).to receive(:warn).with('No test files were executed on this CI node.')101 expect(logger).to receive(:debug).with('This CI node likely started work late after the test files were already executed by other CI nodes consuming the queue.')102 expect(described_class).to receive(:create_build_subset).with([])103 subject104 end105 end106 end107 describe '.create_build_subset' do108 subject { described_class.create_build_subset(test_files) }109 before do110 commit_hash = double111 branch = double112 repository_adapter = instance_double(KnapsackPro::RepositoryAdapters::EnvAdapter, commit_hash: commit_hash, branch: branch)113 expect(KnapsackPro::RepositoryAdapterInitiator).to receive(:call).and_return(repository_adapter)114 unsymbolize_test_files = double115 expect(KnapsackPro::Utils).to receive(:unsymbolize).with(test_files).and_return(unsymbolize_test_files)116 encrypted_test_files = double117 expect(KnapsackPro::Crypto::Encryptor).to receive(:call).with(unsymbolize_test_files).and_return(encrypted_test_files)118 encrypted_branch = double119 expect(KnapsackPro::Crypto::BranchEncryptor).to receive(:call).with(repository_adapter.branch).and_return(encrypted_branch)120 node_total = double121 node_index = double122 expect(KnapsackPro::Config::Env).to receive(:ci_node_total).and_return(node_total)123 expect(KnapsackPro::Config::Env).to receive(:ci_node_index).and_return(node_index)124 action = double125 expect(KnapsackPro::Client::API::V1::BuildSubsets).to receive(:create).with({126 commit_hash: commit_hash,127 branch: encrypted_branch,128 node_total: node_total,129 node_index: node_index,130 test_files: encrypted_test_files,131 }).and_return(action)132 connection = instance_double(KnapsackPro::Client::Connection, success?: success?, errors?: errors?)133 expect(KnapsackPro::Client::Connection).to receive(:new).with(action).and_return(connection).and_return(connection)134 response = double135 expect(connection).to receive(:call).and_return(response)136 end137 shared_examples_for 'create_build_subset method' do138 context 'when success' do139 let(:success?) { true }140 context 'when response has errors' do141 let(:errors?) { true }142 it do143 expect {144 subject145 }.to raise_error(ArgumentError)146 end147 end148 context 'when response has no errors' do149 let(:errors?) { false }150 it do151 logger = instance_double(Logger)152 expect(KnapsackPro).to receive(:logger).and_return(logger)153 expect(logger).to receive(:debug).with('Saved time execution report on Knapsack Pro API server.')154 subject155 end156 end157 end158 context 'when failure' do159 let(:success?) { false }160 let(:errors?) { nil }161 it do162 logger = instance_double(Logger)163 expect(KnapsackPro).to receive(:logger).and_return(logger)164 expect(logger).to receive(:warn).with('Time execution report was not saved on Knapsack Pro API server due to connection problem.')165 subject166 end167 end168 end169 context "when test files doesn't exist" do170 let(:test_files) { [] }171 it_behaves_like 'create_build_subset method'172 end173 context 'when test files exists' do174 let(:test_files) { [double] }175 it_behaves_like 'create_build_subset method'176 end177 end178end...

Full Screen

Full Screen

knapsack_report.rb

Source:knapsack_report.rb Github

copy

Full Screen

...40 tmp_path = "tmp/knapsack/#{report_name}"41 FileUtils.mkdir_p(tmp_path)42 # Use path from knapsack config in case of fallback to master_report.json43 knapsack_report_path = Knapsack.report.report_path44 logger.debug("Moving regenerated #{knapsack_report_path} to save as artifact")45 FileUtils.cp(knapsack_report_path, "#{tmp_path}/#{ENV['CI_NODE_INDEX']}.json")46 end47 # Merge and upload knapsack report to gcs bucket48 #49 # Fetches all files defined in glob and uses parent folder as report name50 #51 # @param [String] glob52 # @return [void]53 def upload_report(glob)54 reports = Pathname.glob(glob).each_with_object(Hash.new { |hsh, key| hsh[key] = [] }) do |report, hash|55 next unless report.extname == ".json"56 hash[report.parent.basename.to_s].push(report)57 end58 return logger.error("Glob '#{glob}' did not contain any valid report files!") if reports.empty?...

Full Screen

Full Screen

report.rb

Source:report.rb Github

copy

Full Screen

1module KnapsackPro2 class Report3 def self.save4 test_files = KnapsackPro.tracker.to_a5 if test_files.empty?6 KnapsackPro.logger.warn("No test files were executed on this CI node.")7 KnapsackPro.logger.debug("When you use knapsack_pro Regular Mode, the reason for no tests executing might be a very narrow tests list. Most likely, you run only tests with a specified tag, and there were fewer test files with the tag than parallel CI nodes.")8 end9 create_build_subset(test_files)10 end11 def self.save_subset_queue_to_file12 test_files = KnapsackPro.tracker.to_a13 subset_queue_id = KnapsackPro::Config::Env.subset_queue_id14 KnapsackPro::Config::TempFiles.ensure_temp_directory_exists!15 FileUtils.mkdir_p(queue_path)16 subset_queue_file_name = "#{subset_queue_id}.json"17 report_path = File.join(queue_path, subset_queue_file_name)18 report_json = JSON.pretty_generate(test_files)19 File.open(report_path, 'w+') do |f|20 f.write(report_json)21 end22 end23 def self.save_node_queue_to_api24 test_files = []25 Dir.glob("#{queue_path}/*.json").each do |file|26 report = JSON.parse(File.read(file))27 test_files += report28 end29 if test_files.empty?30 KnapsackPro.logger.warn("No test files were executed on this CI node.")31 KnapsackPro.logger.debug("This CI node likely started work late after the test files were already executed by other CI nodes consuming the queue.")32 end33 measured_test_files = test_files34 .map { |t| t['time_execution'] }35 .select { |time_execution| time_execution != KnapsackPro::Tracker::DEFAULT_TEST_FILE_TIME }36 if test_files.size > 0 && measured_test_files.size == 037 KnapsackPro.logger.warn("#{test_files.size} test files were executed on this CI node but the recorded time was lost due to:")38 KnapsackPro.logger.warn("1. Please ensure you do not remove the contents of the .knapsack_pro directory between tests run.")39 KnapsackPro.logger.warn("2. Ensure you've added Knapsack::Adapters::RSpecAdapter.bind in your rails_helper.rb or spec_helper.rb. Please follow the installation guide again: https://docs.knapsackpro.com/integration/")40 KnapsackPro.logger.warn("3. Another potential reason for this warning is that all your tests are empty test files, pending tests, or they have syntax errors, and the time execution was not recorded for them.")41 end42 create_build_subset(test_files)43 end44 def self.create_build_subset(test_files)45 repository_adapter = KnapsackPro::RepositoryAdapterInitiator.call46 test_files = KnapsackPro::Utils.unsymbolize(test_files)47 encrypted_test_files = KnapsackPro::Crypto::Encryptor.call(test_files)48 encrypted_branch = KnapsackPro::Crypto::BranchEncryptor.call(repository_adapter.branch)49 action = KnapsackPro::Client::API::V1::BuildSubsets.create(50 commit_hash: repository_adapter.commit_hash,51 branch: encrypted_branch,52 node_total: KnapsackPro::Config::Env.ci_node_total,53 node_index: KnapsackPro::Config::Env.ci_node_index,54 test_files: encrypted_test_files,55 )56 connection = KnapsackPro::Client::Connection.new(action)57 response = connection.call58 if connection.success?59 raise ArgumentError.new(response) if connection.errors?60 KnapsackPro.logger.debug('Saved time execution report on Knapsack Pro API server.')61 else62 KnapsackPro.logger.warn('Time execution report was not saved on Knapsack Pro API server due to connection problem.')63 end64 end65 private66 def self.queue_path67 "#{KnapsackPro::Config::TempFiles::TEMP_DIRECTORY_PATH}/queue/#{KnapsackPro::Config::Env.queue_id}"68 end69 end70end...

Full Screen

Full Screen

save

Using AI Code Generation

copy

Full Screen

1knapsack = Knapsack.new(10, [5, 4, 6, 3], [10, 40, 30, 50])2knapsack.save('knapsack.txt')3knapsack = Knapsack.new(10, [5, 4, 6, 3], [10, 40, 30, 50])4knapsack.save('knapsack.txt')5knapsack = Knapsack.load('knapsack.txt')6 def initialize(max_weight, weights, values)7 def self.load(file_path)8 File.open(file_path, "r") do |file|9 weights = file.readline.split(',').map(&:to_i)10 values = file.readline.split(',').map(&:to_i)11 Knapsack.new(max_weight, weights, values)12 def save(file_path)13 File.open(file_path, "w") do |file|14 file.puts @weights.join(',')15 file.puts @values.join(',')

Full Screen

Full Screen

save

Using AI Code Generation

copy

Full Screen

1 File.open("knapsack.txt", "w") do |f|2 File.open("knapsack.txt", "r") do |f|3 @items << Item.new(name, value.to_i, weight.to_i)4 def self.new_from_file(filename)5 File.open(filename, "r") do |f|6 knapsack.add_item(Item.new(name, value.to_i, weight.to_i))7 def self.new_from_file(filename)8 File.open(filename, "r") do |f|9 knapsack.add_item(Item.new(name, value.to_i, weight.to_i))

Full Screen

Full Screen

save

Using AI Code Generation

copy

Full Screen

1knapsack = Knapsack.new(100)2knapsack.save('knapsack.yml')3knapsack = Knapsack.load('knapsack.yml')

Full Screen

Full Screen

save

Using AI Code Generation

copy

Full Screen

1k.add_item('book', 1, 600)2k.add_item('compass', 2, 9000)3k.add_item('map', 4, 3000)4k.add_item('water', 10, 100)5k.save('knapsack.dat')6k.load('knapsack.dat')7book (1.0kg, 600.0)8compass (2.0kg, 9000.0)9map (4.0kg, 3000.0)10water (10.0kg, 100.0)11k.add_item('book', 1, 600)12k.add_item('compass', 2, 9000)13k.add_item('map', 4, 3000)14k.add_item('water', 10, 100)15File.open('knapsack.dat', 'wb') do |file|16 Marshal.dump(k, file)

Full Screen

Full Screen

save

Using AI Code Generation

copy

Full Screen

1k = Knapsack.new(10)2k.add_item(5, 10)3k.add_item(3, 6)4k.add_item(1, 1)5k.add_item(2, 2)6k.add_item(4, 4)7k.add_item(8, 8)8k.add_item(9, 9)9k.add_item(7, 7)10k.save("knapsack.dat")11k2 = Knapsack.load("knapsack.dat")12 def initialize(capacity)13 def add_item(weight, value)14 @items << Item.new(weight, value)15 @items.each { |item| puts item }16 @items.inject(0) { |total, item| total + item.weight }17 def save(filename)18 File.open(filename, "w") do |f|19 def self.load(filename)20 k = Knapsack.new(0)21 File.open(filename, "r") do |f|22 k.add_item(weight.to_i, value.to_i)23 def initialize(weight, value)

Full Screen

Full Screen

save

Using AI Code Generation

copy

Full Screen

1k = Knapsack.new(10)2k.add_item("A", 3, 5)3k.add_item("B", 2, 3)4k.add_item("C", 4, 7)5k.save("knapsack.dat")6k = Knapsack.load("knapsack.dat")7k = Knapsack.new(10)8k.add_item("A", 3, 5)9k.add_item("B", 2, 3)10k.add_item("C", 4, 7)11k = Knapsack.new(10)12k.add_item("A", 3, 5)13k.add_item("B", 2, 3)14k.add_item("C", 4, 7)15k = Knapsack.new(10)16k.add_item("A",

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful