jaydoane commented on a change in pull request #3779:
URL: https://github.com/apache/couchdb/pull/3779#discussion_r725446210



##########
File path: src/fabric/test/eunit/fabric_tests.erl
##########
@@ -0,0 +1,67 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(fabric_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+
+cleanup_index_files_test_() ->
+  {

Review comment:
       Can you change this file to use 4 spaces for each indentation level?

##########
File path: src/fabric/test/eunit/fabric_tests.erl
##########
@@ -0,0 +1,67 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(fabric_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+
+cleanup_index_files_test_() ->
+  {
+    setup,
+    fun setup/0,
+    fun teardown/1,
+    fun(Ctx) -> [
+      test_cleanup_index_files(),
+      test_cleanup_index_files_with_existing_db(Ctx),
+      test_cleanup_index_files_with_deleted_db(Ctx)
+    ] end
+  }.
+
+
+setup() ->
+  test_util:start_couch([fabric]),
+  meck:new(fabric, [passthrough]),
+  DbName = ?tempdb(),
+  fabric:create_db(DbName),
+  mock_inactive_index_files(DbName),
+  DbName.
+
+
+teardown(Ctx) ->
+  meck:unload(),
+  test_util:stop_couch(Ctx).
+
+
+mock_inactive_index_files(_) ->
+  meck:expect(fabric, inactive_index_files, fun(_) -> [] end).
+
+
+test_cleanup_index_files() ->
+  ?_assertMatch([ok | _], fabric:cleanup_index_files()).

Review comment:
       Maybe a more constrained condition here?

##########
File path: src/fabric/src/fabric.erl
##########
@@ -502,14 +502,18 @@ cleanup_index_files() ->
     [cleanup_index_files(Db) || Db <- Dbs].
 
 %% @doc clean up index files for a specific db
--spec cleanup_index_files(dbname()) -> ok.
+-spec cleanup_index_files(dbname()) -> ok | [].

Review comment:
       This seems a little strange to return either the `ok` atom or the empty 
list. Why not just return `ok`?

##########
File path: src/fabric/test/eunit/fabric_tests.erl
##########
@@ -0,0 +1,67 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(fabric_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+
+cleanup_index_files_test_() ->
+  {
+    setup,
+    fun setup/0,
+    fun teardown/1,
+    fun(Ctx) -> [
+      test_cleanup_index_files(),
+      test_cleanup_index_files_with_existing_db(Ctx),
+      test_cleanup_index_files_with_deleted_db(Ctx)
+    ] end
+  }.
+
+
+setup() ->
+  test_util:start_couch([fabric]),
+  meck:new(fabric, [passthrough]),
+  DbName = ?tempdb(),
+  fabric:create_db(DbName),
+  mock_inactive_index_files(DbName),
+  DbName.
+
+
+teardown(Ctx) ->
+  meck:unload(),
+  test_util:stop_couch(Ctx).
+
+
+mock_inactive_index_files(_) ->
+  meck:expect(fabric, inactive_index_files, fun(_) -> [] end).

Review comment:
       Maybe add a comment explaining why you have to mock this function? And 
does this functionality really belong in a separate function if it's only 
called once from `setup`?
   
   Also noting if I eliminate this function, all tests still pass.

##########
File path: src/fabric/src/fabric.erl
##########
@@ -502,14 +502,18 @@ cleanup_index_files() ->
     [cleanup_index_files(Db) || Db <- Dbs].
 
 %% @doc clean up index files for a specific db
--spec cleanup_index_files(dbname()) -> ok.
+-spec cleanup_index_files(dbname()) -> ok | [].
 cleanup_index_files(DbName) ->
-    lists:foreach(fun(File) ->
-        file:delete(File)
-    end, inactive_index_files(DbName)).
+    try lists:foreach(
+        fun(File) ->
+            file:delete(File)
+        end, inactive_index_files(DbName))
+    catch
+        error:database_does_not_exist -> []
+    end.
 
 %% @doc inactive index files for a specific db
--spec inactive_index_files(dbname()) -> ok.
+-spec inactive_index_files(dbname()) -> options() | {error, Reason :: term()}.

Review comment:
       This change seems unrelated to fixing `cleanup_index_files/1`, so maybe 
better in a separate commit?

##########
File path: src/fabric/test/eunit/fabric_tests.erl
##########
@@ -0,0 +1,67 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(fabric_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+
+cleanup_index_files_test_() ->
+  {
+    setup,
+    fun setup/0,
+    fun teardown/1,
+    fun(Ctx) -> [
+      test_cleanup_index_files(),
+      test_cleanup_index_files_with_existing_db(Ctx),
+      test_cleanup_index_files_with_deleted_db(Ctx)
+    ] end
+  }.
+
+
+setup() ->
+  test_util:start_couch([fabric]),
+  meck:new(fabric, [passthrough]),
+  DbName = ?tempdb(),
+  fabric:create_db(DbName),

Review comment:
       Perhaps add a comment explaining that the deletion/teardown is done in 
one of the tests?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to