Skip to content

Commit 6cf3c67

Browse files
committed
improve docs and warnings
1 parent 49e9b1a commit 6cf3c67

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

lib/scenic/cache/file.ex

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ defmodule Scenic.Cache.File do
4747
and to store the result in your applications code itself. Then during run time, you
4848
compare then pre-computed hash against the run-time of the asset being loaded.
4949
50-
Please take advantage of the helper modules [`Cache.File`](Scenic.Cache.File.html),
51-
[`Cache.Term`](Scenic.Cache.Term.html), and [`Cache.Hash`](Scenic.Cache.Hash.html) to
52-
do this for you. These modules load files and insert them into the cache while checking
53-
a precomputed hash.
54-
5550
These scheme is much stronger when the application code itself is also signed and
5651
verified, but that is an exercise for the packaging tools.
5752
@@ -86,7 +81,6 @@ defmodule Scenic.Cache.File do
8681
8782
end
8883
"""
89-
9084
alias Scenic.Cache
9185
alias Scenic.Cache.Hash
9286

@@ -100,8 +94,9 @@ defmodule Scenic.Cache.File do
10094
* `opts` - a list of options. See below.
10195
10296
Options:
103-
* `hash` - format of the hash. Valid formats include `:sha, :sha224, :sha256, :sha384, :sha512, :ripemd160`
97+
* `hash` - format of the hash. Valid formats include `:sha, :sha224, :sha256, :sha384, :sha512, :ripemd160`. If the hash option is not set, it will use `:sha` by default.
10498
* `scope` - Explicitly set the scope of the asset in the cache.
99+
* `decompress` - if `true` - decompress the data (zlib) after reading and verifying the hash.
105100
106101
On success, returns
107102
`{:ok, cache_key}`
@@ -115,7 +110,9 @@ defmodule Scenic.Cache.File do
115110
# hashes. Is also slower because it has to load the file and compute the hash
116111
# to use as a key even it is is already loaded into the cache.
117112
def load(path, :insecure, opts) do
118-
IO.puts "WARNING: Cache asset loaded as :insecure \"#{path}\""
113+
if Mix.env != :test do
114+
IO.puts "WARNING: Cache asset loaded as :insecure \"#{path}\""
115+
end
119116
with {:ok, data} <- read(path, :insecure, opts) do
120117
hash = Hash.binary(data, opts[:hash] || :sha)
121118

@@ -161,8 +158,8 @@ defmodule Scenic.Cache.File do
161158
* `opts` - a list of options. See below.
162159
163160
Options:
164-
* `hash` - format of the hash. Valid formats include `:sha, :sha224, :sha256, :sha384, :sha512, :ripemd160`
165-
* `decompress` - if true - decompress the data (zlib) after reading and verifying the hash.
161+
* `hash` - format of the hash. Valid formats include `:sha, :sha224, :sha256, :sha384, :sha512, :ripemd160`. If the hash option is not set, it will use `:sha` by default.
162+
* `decompress` - if `true` - decompress the data (zlib) after reading and verifying the hash.
166163
167164
On success, returns
168165
`{:ok, data}`
@@ -175,7 +172,9 @@ defmodule Scenic.Cache.File do
175172
# hashes. Is also slower because it has to load the file and compute the hash
176173
# to use as a key even it is is already loaded into the cache.
177174
def read(path, :insecure, opts) do
178-
IO.puts "WARNING: Cache asset read as :insecure \"#{path}\""
175+
if Mix.env() != :test do
176+
IO.puts "WARNING: Cache asset read as :insecure \"#{path}\""
177+
end
179178
with {:ok, data} <- File.read(path) do
180179
do_unzip(data, opts)
181180
else

test/scenic/cache/file_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ defmodule Scenic.Cache.FileLoadTest do
125125
assert :ets.info(@scope_table) == :undefined
126126
:ets.new(@cache_table, [:set, :public, :named_table])
127127
:ets.new(@scope_table, [:bag, :public, :named_table])
128-
{:ok, data} = Cache.File.read(@sample_path, :insecure)
128+
{:ok, data} = Cache.File.read(@sample_path, @sample_sha)
129129

130130
%{data: data}
131131
end
@@ -161,7 +161,7 @@ defmodule Scenic.Cache.FileLoadTest do
161161
assert Cache.get(@sample_gzip_sha) == data
162162
end
163163

164-
test "load(path, hash) loads gzip with a :sha256 hash", %{data: data} do
164+
test "load(path, hash) loads gzip with a :sha256 hash" do
165165
{:ok, key} =
166166
Cache.File.load(
167167
@sample_gzip_path,
@@ -171,7 +171,7 @@ defmodule Scenic.Cache.FileLoadTest do
171171
)
172172

173173
assert key == @sample_gzip_sha256
174-
assert Cache.get(@sample_gzip_sha256) == data
174+
"sample_file" <> _ = Cache.get(@sample_gzip_sha256)
175175
end
176176

177177
test "load(path, hash) FAILS with invalid :sha hash" do

0 commit comments

Comments
 (0)