Repository: tonybaloney/wily

Issue:
Cannot override targets from wily.cfg
Example wily.cfg file:
```
[wily]
max_revisions = 100
path = ./tests,config.py,./load
```
Run:
`wily build`

Expected behavior:
Generates wily cache. Existing code in the `WilyConfig` class has support for defaulting the targets to the passed path.

Actual behavior:
Prints help message because `targets` are missing.

Possible Solution:
Remove `required=True` from the click option, set `config.targets` only if targets are passed, and do a check that `config.targets` are defined before running `build`.

Discussion:

Patch:
diff --git a/README.md b/README.md
index cc1a540..c50c0e2 100644
--- a/README.md
+++ b/README.md
@@ -137,6 +137,7 @@ maintainability operator:
 You can put a `wily.cfg` file in your project directory and `wily` will override the runtime settings. Here are the available options:
 
 ```ini
+[wily]
 # list of operators, choose from cyclomatic, maintainability, mccabe and raw
 operators = cyclomatic,raw
 # archiver to use, defaults to git
diff --git a/docs/source/commands/build.rst b/docs/source/commands/build.rst
index c1cd7a7..e7311a0 100644
--- a/docs/source/commands/build.rst
+++ b/docs/source/commands/build.rst
@@ -34,7 +34,7 @@ You can override the maximum number of commits recursed by using the ``-n`` or `
 
    $ wily build src/ test/ -n 100
 
-By default, wily will compile the cyclomatic complexity, maintainability and raw metrics. If you only want a subset of those, you can specify with comma-seperate ``--operator`` or ``-o`` flag
+By default, wily will compile the cyclomatic complexity, maintainability and raw metrics. If you only want a subset of those, you can specify with comma-separate ``--operator`` or ``-o`` flag
 
 .. code-block:: none
 
diff --git a/wily/__main__.py b/wily/__main__.py
index d60f01f..4a03e69 100644
--- a/wily/__main__.py
+++ b/wily/__main__.py
@@ -89,7 +89,7 @@ def cli(ctx, debug, config, path, cache):
     type=click.INT,
     help="The maximum number of historical commits to archive",
 )
-@click.argument("targets", type=click.Path(resolve_path=True), nargs=-1, required=True)
+@click.argument("targets", type=click.Path(resolve_path=True), nargs=-1, required=False)
 @click.option(
     "-o",
     "--operators",
@@ -122,8 +122,9 @@ def build(ctx, max_revisions, targets, operators, archiver):
         logger.debug(f"Fixing archiver to {archiver}")
         config.archiver = archiver
 
-    logger.debug(f"Fixing targets to {targets}")
-    config.targets = targets
+    if targets:
+        logger.debug(f"Fixing targets to {targets}")
+        config.targets = targets
 
     build(
         config=config,


Test patch:
diff --git a/test/integration/test_build.py b/test/integration/test_build.py
index 30cf153..688072d 100644
--- a/test/integration/test_build.py
+++ b/test/integration/test_build.py
@@ -6,10 +6,8 @@ Many of the tests will depend on a "builddir" fixture which is a compiled wily c
 
 TODO : Test build + build with extra operator
 """
-import os
 import sys
 import pathlib
-import tempfile
 import pytest
 from click.testing import CliRunner
 from git import Repo, Actor
@@ -62,15 +60,6 @@ def test_build_invalid_path():
     assert result.exit_code == 1, result.stdout
 
 
-def test_build_no_target(tmpdir):
-    """
-    Test that build fails with no target
-    """
-    runner = CliRunner()
-    result = runner.invoke(main.cli, ["--path", tmpdir, "build"])
-    assert result.exit_code == 2, result.stdout
-
-
 def test_build_crash(tmpdir):
     """
     Simulate a runtime error in the build.
@@ -137,6 +126,50 @@ def test_build(tmpdir, cache_path):
     assert rev_path.exists()
 
 
+@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
+def test_build_with_config(tmpdir, cache_path):
+    """
+    Test that build works in a basic repository and a configuration file.
+    """
+    repo = Repo.init(path=tmpdir)
+    tmppath = pathlib.Path(tmpdir)
+
+    config = """
+    [wily]
+    path = test.py
+    """
+    config_path = tmppath / "wily.cfg"
+    with open(config_path, "w") as config_f:
+        config_f.write(config)
+
+    # Write a test file to the repo
+    with open(tmppath / "test.py", "w") as test_txt:
+        test_txt.write("import abc")
+
+    index = repo.index
+    index.add(["test.py"])
+
+    author = Actor("An author", "author@example.com")
+    committer = Actor("A committer", "committer@example.com")
+
+    commit = index.commit("basic test", author=author, committer=committer)
+    repo.close()
+
+    runner = CliRunner()
+    result = runner.invoke(
+        main.cli,
+        ["--debug", "--config", config_path, "--path", tmpdir, "--cache", cache_path, "build"],
+    )
+    assert result.exit_code == 0, result.stdout
+
+    cache_path = pathlib.Path(cache_path)
+    assert cache_path.exists()
+    index_path = cache_path / "git" / "index.json"
+    assert index_path.exists()
+    rev_path = cache_path / "git" / (commit.name_rev.split(" ")[0] + ".json")
+    assert rev_path.exists()
+
+
 def test_build_twice(tmpdir, cache_path):
     """
     Test that build works when run twice.