Repository: spack/spack

Issue:
spack install: --log-file option not behaving as expected (no output without --log-format) in v0.19
### Steps to reproduce

Both the `--test` and `--log-file` options of `spack install PACKAGE` seem to be ignored (or I'm making a huge mistake).
I tried both
```spack install --test all --log-file LOGFILE vim```
as a minimal example with almost no dependencies, and
```spack install --test all --log-file LOGFILE amdlibflame```
which I expect to run post-build tests and dump the logs into a file named LOGFILE in the local directory (or where else should I expect it?), but it seems to do neither of those and the log is located in `$SPACK/opt/spack/$OS/$COMPILER/$PACKAGENAME/.spack/spack-build-out.txt` as always, with no indication of any tests being run, i.e., the behavior seems to be the same as if I hadn't passed the additional options at all.

In addition, I tried the following variations, none of which had any discernible effect:
- absolute path for the logfile, e.g., `/path/to/LOGFILE`
- equals signs, e.g., `--test=all`
- installing in an explicit environment

What I _actually_ want to achieve, by the way, is to get amdlibflame to be built with its post-build checks, so if anyone knows a quick workaround for that in case the command-line options really are bugged, I'd be very grateful for that.

### Error message

No error messages, but the desired outputs/behaviors that I expect from `--test all` and `--log-file LOGFILE` simply do not appear. If I pass a nonsensical option (such as `--rasklasd`) I get a `==> Error: unrecognized arguments: --rasklasd`, so the options _are_ being read, but don't seem to be acted on.

Running the same command with `-ddd` produced no relevant information regarding the tests/`make check` or the location of the log-file.

### Information on your system

* **Spack:** 0.19.0 (a86911246a8c5571ca210ff25fc990b8eb297a5c)
* **Python:** 3.8.10
* **Platform:** linux-ubuntu20.04-zen3
* **Concretizer:** clingo


### General information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have searched the issues of this repo and believe this is not a duplicate
- [X] I have run the failing commands in debug mode and reported the output


Discussion:
Using `--test` does not automagically run `make test` or similar for you; the package class needs to implement a `test` function, which neither `vim` nor `amdlibflame` do per:

```
spack cd -P
grep -Fl 'def test' packages/*/package.py | cut -d/ -f2
```

Among your installed packages you can find packages which support tests using:

```
spack test list
```

Then among your installed package list you can run tests with:

```
spack test run /hash-of-your-spec
spack test results -l
```
I see, thanks for pointing that out. Do you have any suggestion on why the `--log-file` option doesn't work the way I was expecting?

Also, in the `package.py` file of amdlibflame, I found the following snippet, which is what led me to believe there could be some sort of post-build check:
```
    @run_after("build")
    @on_package_attributes(run_tests=True)
    def check(self):
        """make check for single and multithread"""
        blas_flags = self.spec["blas"].libs.ld_flags
        if self.spec.variants["threads"].value != "none":
            make("check", "LIBBLAS = -fopenmp {0}".format(blas_flags), parallel=False)
        else:
            make("check", "LIBBLAS = {0}".format(blas_flags), parallel=False)
```
How do I access this function while installing?
Yes, that's odd that `--log-file` does not output a file in the current directory.

@becker33 running git blame on `./lib/spack/{test/,}cmd/test.py` it looks like you and the BDFL have a hand in the unit tests and implementation of `--log-file`?  Would you please be able to help here with why the command is not outputting logs as expected?

@rkevk Also if you haven't already, you may wish to read relevant section of the package tests documentation because there are several modalities of testing (https://spack.readthedocs.io/en/latest/packaging_guide.html#checking-an-installation).  And yes, to be fair you may not have come across that section of the documentation because it's buried in the packaging section. There's a lot in that testing section which is too much for me to summarize here 😅 
@omsai I removed and re-cloned spack and in 0.20.0.dev0 the `--test` option works as expected (sorry if you saw the now deleted previous post). The `--log-file` behavior is unchanged, though.
@rkevk There were a couple of bugs related to install time tests in v0.19.0 that are now solved in `develop`. The relevant PRs are #33942 and #34531 They are both slated for inclusion in v0.19.1. ~Would that be enough for you to close the issue as solved?~

Ok, I see:
> The --log-file behavior is unchanged, though.

so there's that still to solve


@rkevk For the log files, reading the code it seems that you need to specify the `log-format`[^1], since the default is to use some stubs that do nothing. Did you try to:
```console
spack install --test all --log-file LOGFILE --log-format junit vim
```
? If you're just interested in checking stdout, and not in having CDash or JUnit reports, you can find that in the `<prefix>/.spack/spack-build-out.txt` file.

[^1]: It also seems we could use some polishing of related code :sweat_smile: 
Hi @alalazo: Sorry for the late reply, blame the holidays!
Indeed, at time of writing I thought the issues I had with the two commands were related, hence why I created a single issue, but in retrospect it should have been two issues—anyway, as you noted, the `--test` issue has already been addressed elsewhere.

Regarding `--log-file`, I guess updating the documentation to match the behavior might be sufficient; apart from that, maybe an error should be raised when `--log-file` is requested without specifying `--log-format`, since the current behavior is confusing.
By the way, if I run `spack install --test all vim` as you suggested, I get
```
==> Error: ProcessError: Command exited with status 2:
    'make' '-j1' 'test'
```
but I believe that this is due to what omsai said, namely that the vim `package.py` does not have a test or check method as far as I can tell, so it's probably not of concern (though the error reporting could arguably be improved).

Finally, thanks for the hint regarding `<prefix>/.spack/spack-build-out.txt`, I had already found those files by chance. Did I miss that in the doc or has their existence not been added to the manual? IMO they might briefly be mentioned, e.g., in https://spack.readthedocs.io/en/latest/basic_usage.html#installing-and-uninstalling, since system maintainers will probably want to check the build and install logs from time to time.

Resolving patch:
diff --git a/lib/spack/spack/cmd/install.py b/lib/spack/spack/cmd/install.py
--- a/lib/spack/spack/cmd/install.py
+++ b/lib/spack/spack/cmd/install.py
@@ -340,6 +340,10 @@ def install(parser, args):
     if args.deprecated:
         spack.config.set("config:deprecated", True, scope="command_line")
 
+    if args.log_file and not args.log_format:
+        msg = "the '--log-format' must be specified when using '--log-file'"
+        tty.die(msg)
+
     arguments.sanitize_reporter_options(args)
 
     def reporter_factory(specs):


Test patch: