How to subtract values in fastlane? - fastlane

In my Fastfile I have method:
lane :calculate_build_version do
ENV['BUILD_NUMBER'] - BuildNumberFactory.make
end
BuldNumberFactory code is:
class BuildNumberFactory
class << self
def make
`git rev-list HEAD --count`
end
end
end
During build I get error:
Fastfile:22:in `block (2 levels) in parsing_binding': [!] undefined method `-' for "192":String (NoMethodError)
How can I fix this and perform subtract?

Related

Bitbake do image failure

I have been successfully building an image for many days now. I add all of my custom files to GitLab. I have not knowingly made changes to my build environment. I am now getting errors can can not build my image. Can anyone understand what this error is telling me? I have tried looking it up but nothing seems to work.
Initialising tasks: 100% |##################################################################################################################################################################| Time: 0:00:06
Sstate summary: Wanted 5 Local 3 Network 0 Missed 2 Current 1114 (60% match, 99% complete)
NOTE: Executing Tasks
ERROR: evccapplication-1.0-r0 do_image: Error executing a python function in exec_python_func() autogenerated:
The stack trace of python calls that resulted in this exception/failure was:
File: 'exec_python_func() autogenerated', lineno: 2, function: <module>
0001:
*** 0002:do_image(d)
0003:
File: '/home/michael/Documents/evcc_custom/sources/poky/meta/classes/image.bbclass', lineno: 262, function: do_image
0258:
0259: d.setVarFlag('REPRODUCIBLE_TIMESTAMP_ROOTFS', 'export', '1')
0260: pre_process_cmds = d.getVar("IMAGE_PREPROCESS_COMMAND")
0261:
*** 0262: execute_pre_post_process(d, pre_process_cmds)
0263:}
0264:do_image[dirs] = "${TOPDIR}"
0265:addtask do_image after do_rootfs
0266:
File: '/home/michael/Documents/evcc_custom/sources/poky/meta/lib/oe/utils.py', lineno: 263, function: execute_pre_post_process
0259: for cmd in cmds.strip().split(';'):
0260: cmd = cmd.strip()
0261: if cmd != '':
0262: bb.note("Executing %s ..." % cmd)
*** 0263: bb.build.exec_func(cmd, d)
0264:
0265:# For each item in items, call the function 'target' with item as the first
0266:# argument, extraargs as the other arguments and handle any exceptions in the
0267:# parent thread
File: '/home/michael/Documents/evcc_custom/sources/poky/bitbake/lib/bb/build.py', lineno: 256, function: exec_func
0252: with bb.utils.fileslocked(lockfiles):
0253: if ispython:
0254: exec_func_python(func, d, runfile, cwd=adir)
0255: else:
*** 0256: exec_func_shell(func, d, runfile, cwd=adir)
0257:
0258: try:
0259: curcwd = os.getcwd()
0260: except:
File: '/home/michael/Documents/evcc_custom/sources/poky/bitbake/lib/bb/build.py', lineno: 503, function: exec_func_shell
0499: with open(fifopath, 'r+b', buffering=0) as fifo:
0500: try:
0501: bb.debug(2, "Executing shell function %s" % func)
0502: with open(os.devnull, 'r+') as stdin, logfile:
*** 0503: bb.process.run(cmd, shell=False, stdin=stdin, log=logfile, extrafiles=[(fifo,readfifo)])
0504: except bb.process.ExecutionError as exe:
0505: # Find the backtrace that the shell trap generated
0506: backtrace_marker_regex = re.compile(r"WARNING: Backtrace \(BB generated script\)")
0507: stdout_lines = (exe.stdout or "").split("\n")
File: '/home/michael/Documents/evcc_custom/sources/poky/bitbake/lib/bb/process.py', lineno: 186, function: run
0182:
0183: if pipe.returncode != 0:
0184: if log:
0185: # Don't duplicate the output in the exception if logging it
*** 0186: raise ExecutionError(cmd, pipe.returncode, None, None)
0187: raise ExecutionError(cmd, pipe.returncode, stdout, stderr)
0188: return stdout, stderr
Exception: bb.process.ExecutionError: Execution of '/home/michael/Documents/evcc_custom/build-fb/tmp/work/imx6ull14x14evk-poky-linux-gnueabi/evccapplication/1.0-r0/temp/run.prelink_image.131394' failed with exit code 2
ERROR: Logfile of failure stored in: /home/michael/Documents/evcc_custom/build-fb/tmp/work/imx6ull14x14evk-poky-linux-gnueabi/evccapplication/1.0-r0/temp/log.do_image.131394
ERROR: Task (/home/michael/Documents/evcc_custom/evcc_layers/meta-evccapplication/recipes-core/images/evccapplication.bb:do_image) failed with exit code '1'
NOTE: Tasks Summary: Attempted 3064 tasks of which 3063 didn't need to be rerun and 1 failed.
Summary: 1 task failed:
/home/michael/Documents/evcc_custom/evcc_layers/meta-evccapplication/recipes-core/images/evccapplication.bb:do_image
Summary: There were 2 WARNING messages shown.
Summary: There was 1 ERROR message shown, returning a non-zero exit code.
If you get an error similar to this delete your build folder. Then recreate it. For NXP devices they give you the command
DISTRO=fsl-imx-fb MACHINE=imx6ull14x14evk source imx-setup-release.sh -b build-fb
Save your local.conf and bblayers.bb file to git or to a new location.
Delete the build-fb folder and re-run the command above. Pull in any changes from git or files saved elsewhere.
Re-run bitbake. This got my workspace back. Because you are not deleting any of the downloads folders the build takes little time. All the time is spent compiling sources.

how to to create a mulitline macro in julia?

macro Estruct(name,arg,type,max=100,min=0,descritpion="")
:(struct $(esc(name))
$((esc(arg)))
$(esc(name))($(esc(arg)))=new(
check($(esc(type)),$(esc(arg)),$(esc(name)),$(esc(max)),$(esc(min)),$(esc(descritpion))))
end)
end
how can I imeplent this macro like this:
#Estruct begin
B
arg1
Float64
200.0
5.0
"this"
end
I don't know how to make a multiline macro. I thought I just have to add begin and end, but I'm always getting: MethodError: no method matching var"#Estruct"(::LineNumberNode, ::Module, ::Expr)
There's no such thing as a multiline macro, that's just a macro that takes a block as an argument. You can see how the macro gets invoked by writing a dummy version of it that just returns its arguments:
macro Estruct(args...); args; end
Now you can invoke that the way you want to call it and it will return its arguments as a tuple:
julia> args = #Estruct begin
B
arg1
Float64
200.0
5.0
"this"
end
(quote
#= REPL[12]:2 =#
B
#= REPL[12]:3 =#
arg1
#= REPL[12]:4 =#
Float64
#= REPL[12]:5 =#
200.0
#= REPL[12]:6 =#
5.0
#= REPL[12]:7 =#
"this"
end,)
julia> typeof(args)
Tuple{Expr}
julia> dump(args[1])
Expr
head: Symbol block
args: Array{Any}((12,))
1: LineNumberNode
line: Int64 2
file: Symbol REPL[12]
2: Symbol B
3: LineNumberNode
line: Int64 3
file: Symbol REPL[12]
4: Symbol arg1
5: LineNumberNode
line: Int64 4
file: Symbol REPL[12]
...
8: Float64 200.0
9: LineNumberNode
line: Int64 6
file: Symbol REPL[12]
10: Float64 5.0
11: LineNumberNode
line: Int64 7
file: Symbol REPL[12]
12: String "this"
julia> args[1].args
12-element Vector{Any}:
:(#= REPL[12]:2 =#)
:B
:(#= REPL[12]:3 =#)
:arg1
:(#= REPL[12]:4 =#)
:Float64
:(#= REPL[12]:5 =#)
200.0
:(#= REPL[12]:6 =#)
5.0
:(#= REPL[12]:7 =#)
"this"
This tells you that the macro gets called with a single argument which is an Expr with head type :block, i.e. the one argument is a quoted block. To implement your "multiline macro" you need to implement the macro body to accept a quoted block expression and process it, presumably by looking at its .args field which is where all the expressions you're interested in are. You'll probably want to ignore all the LineNumberNode objects in there and just process the other items in the block.

Using numba in a method to randomize matrices

I'm still not very familiar with numba and my problem is that I have the piece of code bellow that I use for randomize the edges of graphs.
This code is simply used to swap some edges in a connectivity matrix given the number of desired swaps and a seed for the random number generator.
My problem is that when I try to use it with numba to speed it up I did not menage to run it. The error it returns is also pasted bellow.
#nb.jit(nopython=True)
def _randomize_adjacency_wei(A, n_swaps, seed):
np.random.seed(seed)
# Number of nodes
n_nodes = A.shape[0]
# Copy the adj. matrix
Arnd = A.copy()
# Choose edges that will be swaped
edges = np.random.choice(n_nodes, size=(4, n_swaps), replace=True).T
#itr = range(n_swaps)
#for it in tqdm(itr) if verbose else itr:
it = 0
for it in range(n_swaps):
i,j,k,l = edges[it,:]
if len(np.unique([i,j,k,l]))<4:
continue
else:
# Old values of weigths
w_ij,w_il,w_kj,w_kl=Arnd[i,j],Arnd[i,l],Arnd[k,j],Arnd[k,l]
# Swaping edges
Arnd[i,j]=Arnd[j,i]=w_il
Arnd[k,l]=Arnd[l,k]=w_kj
Arnd[i,l]=Arnd[l,i]=w_ij
Arnd[k,j]=Arnd[j,k]=w_kl
return Arnd
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function unique at 0x7f1a1c03b0d0>) found for signature:
>>> unique(list(int64)<iv=None>)
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload in function 'np_unique': File: numba/np/arrayobj.py: Line 1915.
With argument(s): '(list(int64)<iv=None>)':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'ravel' of type list(int64)<iv=None>
File "../../../home/vinicius/anaconda3/lib/python3.8/site-packages/numba/np/arrayobj.py", line 1918:
def np_unique_impl(a):
b = np.sort(a.ravel())
^
During: typing of get attribute at /home/vinicius/anaconda3/lib/python3.8/site-packages/numba/np/arrayobj.py (1918)
File "../../../home/vinicius/anaconda3/lib/python3.8/site-packages/numba/np/arrayobj.py", line 1918:
def np_unique_impl(a):
b = np.sort(a.ravel())
^
raised from /home/vinicius/anaconda3/lib/python3.8/site-packages/numba/core/typeinfer.py:1071
During: resolving callee type: Function(<function unique at 0x7f1a1c03b0d0>)
During: typing of call at <ipython-input-165-90ffd30fe0e8> (19)
File "<ipython-input-165-90ffd30fe0e8>", line 19:
def _randomize_adjacency_wei(A, n_swaps, seed):
<source elided>
i,j,k,l = edges[it,:]
if len(np.unique([i,j,k,l]))<4:
^
Thanks in advance,
Vinicius
According to the comments, you are passing a list to np.unique() but this is not supported by Numba.
Modifying the code this way:
i, j, k, l = e = edges[it, :]
if len(np.unique(e)) < 4:
...
The following example doesn't produce any errors:
>>> A = np.random.randint(0, 5, (8,8))
>>> r = _randomize_adjacency_wei(A, 4, 33)

Protected execution, 2 cases

Why does in first following case protected execution work, but in second does not?:
q)t:([]a:1 2;b:3 4);
q)#[#[cols t; ; :; `bb]; (cols t)?`b; `columnNotFound]
`a`bb
q)#[#[cols t; ; :; `cc]; (cols t)?`c; `columnNotFound] // 1. works perfectly
`columnNotFound
q)#[#[cols t; (cols t)?`c; :; `cc]; `; `columnNotFound] // 2. exception does not handled
'length
[0] #[#[cols t; (cols t)?`c; :; `cc]; `; `columnNotFound]
^
Upd:
Hmm, I suspect something after trying:
q)#[{#[cols t; (cols t)?`c; :; `cc]}; `; `columnNotFound]
`columnNotFound
The protected execution is using the argument you're supplying. The first two examples are projections but the last is not, so it fails on execution.
q){#[cols t;x;:;`bb]}(cols t)?`b
`a`bb
q){#[cols t;x;:;`cc]}(cols t)?`c / thrown into error trap
'length
[1] {#[cols t;x;:;`cc]}
^
q))\
q)#[cols t;(cols t)?`c;:;`cc] / fails on execution
'length
[0] #[cols t;(cols t)?`c;:;`cc]
^
q)
In your upd, making the # apply a function is forcing the argument in the protected execution to be used.
q){#[cols t;(cols t)?`c;:;`cc]}`
'length
[1] {#[cols t;(cols t)?`c;:;`cc]}
^
q))

Test passes with `testrb`, but crashes using Rails `rake test`?

I have a simple test to check that a carrierwave uploader works.
I use minitest for this, and the test works when run on its own, but not under Rails’s rake test... environment.
(Code for the test is included below.)
Things that work:
If I run ruby test/uploaders/image_file_uploader_test.rb the test passes.
If I run testrb test/uploaders/image_file_uploader_test.rb the test passes.
If I manually call all the lines in the test from IRB (not the Rails console), the code does what’s expected
If I create a small Rake TestTask to run the file (task also copied below), the test passes.
Things that do not work:
If I call rake test test/uploaders/image_file_uploader_test.rb I get dropped into the debugger (stack trace below)
If I call zeus rake test test/uploaders/image_file_uploader_test.rb I also get dropped into the debugger
What is strange:
If I head up the call stack in the debugger to the line in the test which has caused the failure – uploader.store!(#file) – and call it directly with (rdb:1) p uploader.store!(#file), it works! By which I mean, the method returns as expected and the file appears in the correct directory.
Thoughts?
I may be doing something really dumb here. It must be something to do with the Rake task loading the Rails environment, right? Am I doubly-including things or something? Is it something to do with the initializer in the Rails environment? (N.B., when I execute store! from the debugger, it stores the file in the location specified in the test, so it is overridding the Rails initializer successfully).
The backtraces
From bundle exec rake test test/uploaders/*_test.rb
# Running tests:
/Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75: `' (NilClass)
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:926:in `_run_suite'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `block in _run_suites'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `map'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1072:in `block in _run'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1071:in `each'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1071:in `_run'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:795:in `block in autorun'
/Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75:
(rdb:1) where
--> #1 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75:in `rmdir'
#2 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:61:in `with_callbacks'
#3 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:58:in `store!'
#4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file'
#5 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1258:in `run'
#6 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:933:in `_run_suite'
#7 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites'
#8 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything'
#9 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests'
#10 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1072:in `_run'
#11 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
(rdb:1) up 3
#4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file'
(rdb:1) list
[33, 42] in /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb
33 end
34
35 # The whole point of this is to upload a file.
36 def test_upload_of_file
37 uploader = ImageFileUploader.new
=> 38 uploader.store!(#file)
39 # Ensure the uploaded file is correct.
40 assert_equal Digest::SHA2.file(#file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest
41 end
42
(rdb:1) p uploader.store!(#file)
[:store_versions!]
(rdb:1)
After the call to p uploader.store!(#file), the file has been saved
From zeus test test/uploaders/image_file_uploader_test.rb (this is pretty similar to the above)
/Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75: `' (NilClass)
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (3 levels) in go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `fork'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (2 levels) in go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `each'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (3 levels) in go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `fork'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:78:in `block (2 levels) in go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `each'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:73:in `block in go'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `loop'
from /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:62:in `go'
from -e:1:in `<main>'
/Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75:
(rdb:1) where
--> #1 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:75:in `rmdir'
#2 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:61:in `with_callbacks'
#3 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:58:in `store!'
#4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file'
#5 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1258:in `run'
#6 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:933:in `_run_suite'
#7 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/parallel_each.rb:71:in `_run_suites'
#8 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:877:in `_run_anything'
#9 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1085:in `run_tests'
#10 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1072:in `_run'
#11 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
#12 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus/m.rb:203:in `execute'
#13 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus/m.rb:121:in `run'
#14 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus/m.rb:106:in `run'
#15 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus/rails.rb:190:in `test'
#16 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:116:in `command'
#17 /Users/leo/.rvm/gems/ruby-2.0.0-p195#portfolio_site/gems/zeus-0.13.3/lib/zeus.rb:80:in `go'
(rdb:1) up 3
#4 /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb:38:in `test_upload_of_file'
(rdb:1) list
[33, 42] in /Users/leo/Projects/PortfolioSite/test/uploaders/image_file_uploader_test.rb
33 end
34
35 # The whole point of this is to upload a file.
36 def test_upload_of_file
37 uploader = ImageFileUploader.new
=> 38 uploader.store!(#file)
39 # Ensure the uploaded file is correct.
40 assert_equal Digest::SHA2.file(#file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest
41 end
42
(rdb:1) p uploader.store!(#file)
[:store_versions!]
(rdb:1)
The code
Class being tested
class ImageFileUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
version :thumbnail do
process resize_to_fill: [100,100]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
The test itself
require 'minitest/autorun'
require 'minitest/pride'
require 'minitest/debugger' if ENV['DEBUG']
require 'rmagick'
require 'carrierwave'
require_relative '../../app/uploaders/image_file_uploader'
class ImageFileUploaderTest < MiniTest::Unit::TestCase
# Before any tests run, set up parameters.
FILENAME = 'test_photo_1.jpg'
STORE_DIR = 'tmp/uploads/store'
CACHE_DIR = 'tmp/uploads/cache'
STORE_PATH = File.join __dir__, '..', '..', STORE_DIR
CACHE_PATH = File.join __dir__, '..', '..', CACHE_DIR
# Override the store and cache dirs so we’re not reliant on Rails.
class ::ImageFileUploader
storage :file
store_dir STORE_PATH
cache_dir CACHE_PATH
end
# Before each test runs, set up a file to test with.
def setup
#file = File.new "#{__dir__}/../test_files/#{FILENAME}"
end
# After each test runs, clear the results directory so it doesn't influence other tests.
def teardown
FileUtils.rm_rf STORE_PATH
FileUtils.rm_rf CACHE_PATH
end
# The whole point of this is to upload a file.
def test_upload_of_file
uploader = ImageFileUploader.new
uploader.store!(#file)
# Ensure the uploaded file is correct.
assert_equal Digest::SHA2.file(#file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest
end
end
Simple Rake task
require 'rake/testtask'
Rake::TestTask.new('dev:test') do |t|
t.test_files = FileList['test/uploaders/*_test.rb']
end
Rails initializer (N.B. I think the test is isolated from this...)
CarrierWave.configure do |config|
config.storage = :file
# Override the directory where uploaded files will be stored.
config.store_dir = -> do
if model.nil?
"uploads/other/#{Time.now.strftime("%F")}/#{Time.now.strftime("%H-%M-%S")}"
else
# This is a sensible default for uploaders that are meant to be mounted:
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# Override the directory where temp files will be stored.
config.cache_dir = -> do
# This is a better default because it prevents temp files from becoming public and is more consistent with the Rails directory structure.
Rails.root.join('tmp/uploads')
end
end
Thanks!
I have fixed this, although not through an entirely scientific process as I was changing other things in the code, including the overall Rails environment, as I went along.
The current, working version greatly simplifies the Carrierwave initializer, moving path definition to the uploader, and then monkey-patching those methods in the test.
Annoyingly, the original reason for the initializer-based approach was that custom directories were being picked up for the original image, but not for processed versions. That seems to be working fine now, but how this current code differs from the old one I’m not sure.
Rails initializer
CarrierWave.configure do |config|
config.storage = :file
end
Uploader
class ImageFileUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
version :thumbnail do
process resize_to_fill: [100,100]
end
def extension_white_list
%w(jpg jpeg gif png)
end
def store_dir
if model.nil?
"uploads/other/#{Time.now.strftime("%F")}/#{Time.now.strftime("%H-%M-%S")}"
else
# This is a sensible default for uploaders that are meant to be mounted:
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
def cache_dir
# This is a better default because it prevents temp files from becoming public and is more consistent with the Rails directory structure.
Rails.root.join('tmp/uploads')
end
end
Test
require_relative '../test_helper'
require 'rmagick'
require 'carrierwave'
require_relative '../../app/uploaders/image_file_uploader'
class ImageFileUploaderTest < MiniTest::Unit::TestCase
# Before any tests run, set up parameters.
FILENAME = 'test_photo_1.jpg'
STORE_DIR = 'tmp/uploads/store'
CACHE_DIR = 'tmp/uploads/cache'
STORE_PATH = File.join __dir__, '..', '..', STORE_DIR
CACHE_PATH = File.join __dir__, '..', '..', CACHE_DIR
# Override the store and cache dirs so we’re not reliant on Rails.
class ::ImageFileUploader
storage :file
def store_dir; STORE_PATH; end
def cache_dir; CACHE_PATH; end
end
# Before each test runs, set up a file to test with.
def setup
#file = File.new "#{__dir__}/../test_files/#{FILENAME}"
end
# After each test runs, clear the results directory so it doesn't influence other tests.
def teardown
FileUtils.rm_rf STORE_PATH
FileUtils.rm_rf CACHE_PATH
end
# The whole point of this is to upload a file.
def test_upload_of_file
uploader = ImageFileUploader.new
uploader.store!(#file)
# Ensure the uploaded file is correct.
assert_equal Digest::SHA2.file(#file).hexdigest, Digest::SHA2.file("#{STORE_PATH}/#{FILENAME}").hexdigest
end
# After each test, there is nothing to clean up, as teardown clears the output dir.
def after_tests
end
end
(N.B. The inclusion of test_helper is not relevant to the fix – after I had it working I moved some of the requires up to the helper to DRY up some overlap with other tests.)