diff --git a/bin/cache.ml b/bin/cache.ml index 079c2e38499d..e2dbf9e84456 100644 --- a/bin/cache.ml +++ b/bin/cache.ml @@ -52,17 +52,39 @@ let trim = (User_message.make [ Pp.textf "Freed %s" (Bytes_unit.pp trimmed_bytes) ]) +let size = + let info = + let doc = "Query the size of the Dune cache" in + let man = + [ `P "Output the size of the Dune cache in a human-readable form." ] + in + Cmd.info "size" ~doc ~man + in + Cmd.v info + @@ let+ machine_readble = + Arg.( + value & flag + & info [ "machine-readable" ] + ~doc:"Outputs size as a plain number of bytes.") + in + let size = Dune_cache.Trimmer.overhead_size () in + if machine_readble then + User_message.print (User_message.make [ Pp.textf "%Ld" size ]) + else + User_message.print + (User_message.make [ Pp.textf "%s" (Bytes_unit.pp size) ]) + let command = let info = let doc = "Manage the shared cache of build artifacts" in let man = [ `S "DESCRIPTION" ; `P - "Dune can share build artifacts between workspaces. Currently, the \ - only action supported by this command is `trim`, but we plan to \ - provide more functionality soon." + "Dune can share build artifacts between workspaces. We currently \ + only support a few subcommands however we plan to provide more \ + functionality soon." ] in Cmd.info "cache" ~doc ~man in - Cmd.group info [ trim ] + Cmd.group info [ trim; size ] diff --git a/test/blackbox-tests/test-cases/dune-cache/cache-man.t b/test/blackbox-tests/test-cases/dune-cache/cache-man.t index 4c521db6cfaa..30bb82485ab9 100644 --- a/test/blackbox-tests/test-cases/dune-cache/cache-man.t +++ b/test/blackbox-tests/test-cases/dune-cache/cache-man.t @@ -8,53 +8,14 @@ Here we observe the documentation for the dune cache commands. dune cache COMMAND … DESCRIPTION - Dune can share build artifacts between workspaces. Currently, the only - action supported by this command is `trim`, but we plan to provide - more functionality soon. + Dune can share build artifacts between workspaces. We currently only + support a few subcommands however we plan to provide more + functionality soon. COMMANDS - trim [--size=BYTES] [--trimmed-size=BYTES] [OPTION]… - Trim the Dune cache - - COMMON OPTIONS - --help[=FMT] (default=auto) - Show this help in format FMT. The value FMT must be one of auto, - pager, groff or plain. With auto, the format is pager or plain - whenever the TERM env var is dumb or undefined. - - --version - Show version information. + size [--machine-readable] [OPTION]… + Query the size of the Dune cache - EXIT STATUS - cache exits with the following status: - - 0 on success. - - 123 on indiscriminate errors reported on standard error. - - 124 on command line parsing errors. - - 125 on unexpected internal errors (bugs). - - SEE ALSO - dune(1) - - -Man pages of the deprecated start and stop commands. - - $ dune cache start --help=plain - NAME - dune-cache - Manage the shared cache of build artifacts - - SYNOPSIS - dune cache COMMAND … - - DESCRIPTION - Dune can share build artifacts between workspaces. Currently, the only - action supported by this command is `trim`, but we plan to provide - more functionality soon. - - COMMANDS trim [--size=BYTES] [--trimmed-size=BYTES] [OPTION]… Trim the Dune cache @@ -81,22 +42,20 @@ Man pages of the deprecated start and stop commands. SEE ALSO dune(1) +Testing the output of dune cache size --machine-readable - $ dune cache stop --help=plain + $ dune cache size --help=plain NAME - dune-cache - Manage the shared cache of build artifacts + dune-cache-size - Query the size of the Dune cache SYNOPSIS - dune cache COMMAND … + dune cache size [--machine-readable] [OPTION]… - DESCRIPTION - Dune can share build artifacts between workspaces. Currently, the only - action supported by this command is `trim`, but we plan to provide - more functionality soon. + Output the size of the Dune cache in a human-readable form. - COMMANDS - trim [--size=BYTES] [--trimmed-size=BYTES] [OPTION]… - Trim the Dune cache + OPTIONS + --machine-readable + Outputs size as a plain number of bytes. COMMON OPTIONS --help[=FMT] (default=auto) @@ -108,7 +67,7 @@ Man pages of the deprecated start and stop commands. Show version information. EXIT STATUS - cache exits with the following status: + size exits with the following status: 0 on success. @@ -121,7 +80,6 @@ Man pages of the deprecated start and stop commands. SEE ALSO dune(1) - Testing the output of dune cache trim. $ dune cache trim --help=plain diff --git a/test/blackbox-tests/test-cases/dune-cache/size.t/config b/test/blackbox-tests/test-cases/dune-cache/size.t/config new file mode 100644 index 000000000000..d038108eb49c --- /dev/null +++ b/test/blackbox-tests/test-cases/dune-cache/size.t/config @@ -0,0 +1,3 @@ +(lang dune 3.7) +(cache enabled) +(cache-storage-mode copy) diff --git a/test/blackbox-tests/test-cases/dune-cache/size.t/dune b/test/blackbox-tests/test-cases/dune-cache/size.t/dune new file mode 100644 index 000000000000..0382df43f0ad --- /dev/null +++ b/test/blackbox-tests/test-cases/dune-cache/size.t/dune @@ -0,0 +1,6 @@ +(rule + (targets target_a) + (action + (with-outputs-to + target_a + (echo Hello World!)))) diff --git a/test/blackbox-tests/test-cases/dune-cache/size.t/dune-project b/test/blackbox-tests/test-cases/dune-cache/size.t/dune-project new file mode 100644 index 000000000000..3c48133ad585 --- /dev/null +++ b/test/blackbox-tests/test-cases/dune-cache/size.t/dune-project @@ -0,0 +1 @@ +(lang dune 3.7) diff --git a/test/blackbox-tests/test-cases/dune-cache/size.t/run.t b/test/blackbox-tests/test-cases/dune-cache/size.t/run.t new file mode 100644 index 000000000000..92f42916dc70 --- /dev/null +++ b/test/blackbox-tests/test-cases/dune-cache/size.t/run.t @@ -0,0 +1,26 @@ +This test checks that the dune cache size command returns the correct size of +the cache. + + $ export DUNE_CACHE=enabled + $ export DUNE_CACHE_ROOT=$PWD/.cache + +We build a simple file which the contents of "Hello World". + + $ dune build target_a --display=short + +Now we remove it so that we are checking the size of the file rather than the +link Dune created. + + $ rm _build/default/target_a + +The size command reports the size of the file in bytes in human readable form. +It correctly reports 12 bytes. + + $ dune cache size + 12B + +We also have a machine readable version of the command which reports the size in +bytes directly without any units. + + $ dune cache size --machine-readable + 12 diff --git a/test/blackbox-tests/test-cases/dune-cache/trim.t b/test/blackbox-tests/test-cases/dune-cache/trim.t index 9dba83638720..59d0b4f6a283 100644 --- a/test/blackbox-tests/test-cases/dune-cache/trim.t +++ b/test/blackbox-tests/test-cases/dune-cache/trim.t @@ -192,5 +192,3 @@ are part of the same rule. TODO: Test trimming priority in the [copy] mode. In PR #4497 we added a test but it turned out to be flaky so we subsequently deleted it in #4511. - -