Ruby require_relative doesn't work in Lambda - ruby

I've got Ruby lambda handler in which I want to include the files responsible for running the whole lambda functionality. These files are in src/config/environment.rb
src
|
|config
|
|environment.rb
The lambda handler is inside src/Quiz_continue/app.rb
src
|
|Quiz_continue
|
|app.rb
So to include environment.rb file what I did was to use require_relative (which worked in unit test) like below:
require_relative "../config/environment"
def lambda_handler(event:, context:)
#some lambda functionality
end
But when I want to test Lambda locally using sam build and sam local start-api commands I'm getting below error:
Invalid lambda response received: Invalid API Gateway Response Keys: {'stackTrace', 'errorType', 'errorMessage'} in {'errorMessage': 'cannot load such file -- /var/config/environment', 'errorType': 'Init', 'stackTrace': ["/var/task/app.rb:3:in require_relative'", "/var/task/app.rb:3:in <top (required)>'", "/var/lang/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:85:in require'", "/var/lang/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:85:in require'"]}
Where am I wrong?

Related

RSpec auto loading behavior for gems

I have a gem project with rspec configured. My folder structure looks like this -
lib/
- foo/
- foo.rb
- alpha.rb
spec/
- spec_helper.rb
integration/
- alpha_spec.rb
The files foo.rb and alpha.rb have the following structures in terms of class definitions.
# lib/foo/foo.rb
class Foo
# do stuff
end
and
# lib/foo/alpha.rb
require_relative 'foo'
class Foo
class Alpha
# do stuff
end
end
The main takeaway here is that Alpha is a nested class that requires 'foo.rb' directly. (Someone using it would require foo/alpha from their script)
My spec_helper.rb file simply requires my library by loading foo/alpha -
# spec/spec_helper.rb
# Check if `Foo` class is already defined
Object.const_defined?('Foo') ? puts "IS DEFINED" : "IS NOT DEFINED"
require 'foo/alpha'
To my surprise, the constant Foo was already loaded even before requiring alpha/foo, as the output returned IS DEFINED.
Because of this, my require statement tries to load alpha.rb which in turn requires foo.rb and that errors out with
foo.rb:1:in `<top (required)>': Foo is not a class (TypeError)
According to this thread, that type of error is raised by RSpec when a class (Foo) is already defined.
How is this happening? Does RSpec attempt to do some magic behind the scenes and load my library? How do I get around this?
EDIT: I also removed the --require spec_helper line from my .rspec file so it's only getting loaded manually when I run the test.

Error when trying to run rspec Failure/Error: require 'test' LoadError: cannot load such file -- test

I am trying to run RSpec for Ruby on Rails. I am running Rails 5.0 I have installed the gem, have established a spec folder with some tests.
I run following command on console
$ rspec --init
create spec/spec_helper.rb
create .rspec
I create file called 'test.rb'.
class Test
end
I create file called 'zombie_spec.rb'.
require 'spec_helper'
require 'test'
describe Test do
it " Name Is Bapu." do
# test = test.new
# test.name.should == 'bapu'
end
end
Then after I run this command.
rspec spec/lib/zombie_spec.rb
It shows error ⬇
An error occurred while loading ./spec/lib/zombie_spec.rb.
Failure/Error: require 'test'
LoadError:
cannot load such file -- test
# ./spec/lib/zombie_spec.rb:2:in `require'
# ./spec/lib/zombie_spec.rb:2:in `<top (required)>'
No examples found.
Finished in 0.00026 seconds (files took 0.40148 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
How do I resolve this so that I can start running tests?
Your file paths are off, you either need to have zombie_spec.rb in the spec/ folder instead of spec/lib/ or you need to move test.rb into the lib/ folder instead of being in the / project root folder.

Buildr, RSpec—pass build parameter to test

I have »buildr« »buildfile« which triggers some »rspec« tests. I would like to pass some path parameters to the tests, so that It wont cause trouble to load test-resources files. In the »buildfile« I have got this code to trigger the tests:
RSpec.configure do |config|
config.add_setting :spec_resources_dir, :default => _(:src, 'spec', 'ruby', 'resources')
end
RSpec::Core::RakeTask.new(:run_rspec) do |t|
t.pattern = 'src/spec/**/*_spec.rb'
end
task test => [:run_rspec]
But if I try to retrieve the value in the specfile like this:
RSpec.configuration.spec_resources_dir
I get this error
undefined method `spec_resources_dir' […] (NoMethodError)
Any ideas?
RSpec's rake task runs the specs in a separate process, so configuration you do with RSpec.configure in the buildfile will not be visible to the running specs.
Two suggestions for passing info from the buildfile to your spec task:
Generate a spec_helper and require it from your specs (or via rspec's -r command line option and the rspec_opts config parameter on RSpec::Core::RakeTask). You could use buildr's filtering to substitute values from the buildfile into the helper.
Set values in ENV and then read them out from your specs. Environment variables are shared from parent to child processes.
By request, an example for #1:
RSpec::Core::RakeTask.new do |t|
t.rspec_opts = "-r '#{_(:target, 'spec_helper.rb')}'"
end
This assumes that you (probably in another task) generate the spec helper into _(:target, 'spec_helper.rb')

sinatra -- unknown media type

I am writing a blog for games, loading files with extension *.sgf Sinatra doesn't recognize this.
Unknown media type: ".sgf" file: base.rb location: content_type line: 132
The backtrace mentions webrick
/usr/lib/ruby/1.9.1/webrick/httpserver.rb in service
si.service(req, res) /usr/lib/ruby/1.9.1/webrick/httpserver.rb in run
server.service(req, res) /usr/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
block ? block.call(sock) : run(sock)
I caught this since although my Sinatra app works when I do ruby myApp.rb it doesn't work when I do foreman start for Heroku (and it didn't work when I deployed).
You should configure Sinatra to understand your MIME-type:
configure do
mime_type :sgf, 'application/octet-stream'
end
or inplace:
get '/upload' do
content_type :sgf
# Do what you want with the file
end
More info.

Error "undefined local variable or method `logger'" when using `logger.info` in a Sinatra application

I have the following Sinatra 1.2.1 application code:
# app.rb
require 'sinatra'
get '/' do
logger.info "COUCOU"
'Hello world!'
end
and start the server with ruby -rubygems app.rb. When I go to http://localhost:4567 I get the error:
NameError at /
undefined local variable or method `logger' for #<Sinatra::Application:0x00000100d91f88>
file: app.rb location: block in <main> line: 4
Do I need to add or configure something to enable logging in Sinatra? Reading the Sinatra README and documentation, it looks like logging is enabled by default for Sinatra::Application.
The problem is in the not found write method, just extend the Logger class this way and everything should be ok:
class Logger
# Make Rack::CommonLogger accept a Logger instance
# without raising undefined method `write' for #<Logger:0x007fc12db61778>
# makes a method alias using symbols
alias :write :<<
end
You are probably missing a logger = Logger.new.
http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/
Logger is not defined, to overcome it you can just use
Rails.logger.info "COUCOU"
or define it like this:
logger = Rails.logger.new
logger.info "COUCOU"

Resources