# How to check if a file was created by a test?

**URL:** <https://discourse.cmake.org/t/how-to-check-if-a-file-was-created-by-a-test/7017>\
**Category:** Code\
**Created:** [December 6, 2022, 4:24am UTC](https://discourse.cmake.org/t/how-to-check-if-a-file-was-created-by-a-test/7017 "2022-12-06T04:24:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)\
**Post date:** [December 6, 2022, 4:24am UTC](https://discourse.cmake.org/t/how-to-check-if-a-file-was-created-by-a-test/7017/1 "2022-12-06T04:24:09Z")

</div>

Say I have an executable that creates a file. What’s the best way to write a test to verify the **existence** of the file.

```auto
# Assume the file is created in the same directory as the executable
add_test(NAME create_json_file COMMAND json_creator --name "foobar.json")

```

Currently the best idea I have is to write a 2nd test, that depends on the prior test.  
I used the builtin `cat` command since it fails when the file doesn’t exist.

```auto
add_test(NAME check_json_exists
    COMMAND ${CMAKE_COMMAND} -E cat $<TARGET_FILE_DIR:json_creator>/foobar.json
)
set_tests_properties(check_json_exists PROPERTIES DEPENDS create_json_file)

```

Is there a more idiomatic way? I think this is pretty clean, but I’m curious.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [December 6, 2022, 7:30am UTC](https://discourse.cmake.org/t/how-to-check-if-a-file-was-created-by-a-test/7017/2 "2022-12-06T07:30:57Z")

</div>

I would create a wrapper script that executes your program and then exits with a failure code if the file does not exist. Then register that wrapper script as the actual test.

The wrapper script could easily be written in cmake itself.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [December 6, 2022, 5:59pm UTC](https://discourse.cmake.org/t/how-to-check-if-a-file-was-created-by-a-test/7017/3 "2022-12-06T17:59:08Z")

</div>

You also want to remove the file at the start of the test so that previously passing runs don’t mask new failures.
