From b30413dd41ccc558c8e799ab0f7c6238e8623f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Enr=C3=ADquez?= Date: Thu, 22 Dec 2022 13:20:05 +0100 Subject: [PATCH 1/3] [#254] Revise dump-config command Problem: xrefcheck does not allow to print the config to stdout instead of writing it to a file. Also, it is easy to overwrite your changes by mistake by executing the command again. Solution: provide a --stdout flag to print the config to stdout, and do not write it to a file unless a --force flag has been included. --- CHANGES.md | 3 + exec/Main.hs | 10 ++- package.yaml | 1 + src/Xrefcheck/CLI.hs | 25 ++++++- tests/golden/check-dump-config/.config.yaml | 3 + .../golden/check-dump-config/.xrefcheck.yaml | 3 + .../check-dump-config/check-dump-config.bats | 75 +++++++++++++++++++ 7 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 tests/golden/check-dump-config/.config.yaml create mode 100644 tests/golden/check-dump-config/.xrefcheck.yaml create mode 100644 tests/golden/check-dump-config/check-dump-config.bats diff --git a/CHANGES.md b/CHANGES.md index a4d037eb..de5363f1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,6 +41,9 @@ Unreleased * [#231](https://github.com/serokell/xrefcheck/pull/231) + Anchor analysis takes now into account the appropriate case-sensitivity depending on the configured Markdown flavour. +* [#254](https://github.com/serokell/xrefcheck/pull/254) + + Now the `dump-config` command does not overwrite a file unless explicitly told with a + `--force` flag. Also, a `--stdout` flag allows to print the config to stdout instead. 0.2.2 ========== diff --git a/exec/Main.hs b/exec/Main.hs index 404dab35..0eec87ce 100644 --- a/exec/Main.hs +++ b/exec/Main.hs @@ -10,7 +10,8 @@ import Universum import Main.Utf8 (withUtf8) import System.IO.CodePage (withCP65001) -import Xrefcheck.CLI (Command (..), getCommand) +import System.Directory (doesFileExist) +import Xrefcheck.CLI (Command (..), DumpConfigMode (..), getCommand) import Xrefcheck.Command (defaultAction) import Xrefcheck.Config (defConfigText) @@ -20,5 +21,10 @@ main = withUtf8 $ withCP65001 $ do case command of DefaultCommand options -> defaultAction options - DumpConfig repoType path -> + DumpConfig repoType (DCMFile forceFlag path) -> do + whenM ((not forceFlag &&) <$> doesFileExist path) do + putTextLn "Output file exists. Use --force to overwrite." + exitFailure writeFile path (defConfigText repoType) + DumpConfig repoType DCMStdout -> + putStr (defConfigText repoType) diff --git a/package.yaml b/package.yaml index b497b20c..2427c59e 100644 --- a/package.yaml +++ b/package.yaml @@ -130,6 +130,7 @@ executables: dependencies: - xrefcheck - universum + - directory - with-utf8 - code-page diff --git a/src/Xrefcheck/CLI.hs b/src/Xrefcheck/CLI.hs index b06992e7..622aec4c 100644 --- a/src/Xrefcheck/CLI.hs +++ b/src/Xrefcheck/CLI.hs @@ -9,6 +9,7 @@ module Xrefcheck.CLI ( VerifyMode (..) , ExclusionOptions (..) , Command (..) + , DumpConfigMode (..) , Options (..) , NetworkingOptions (..) @@ -70,7 +71,11 @@ modes = data Command = DefaultCommand Options - | DumpConfig Flavor FilePath + | DumpConfig Flavor DumpConfigMode + +data DumpConfigMode + = DCMFile Bool FilePath + | DCMStdout data Options = Options { oConfigPath :: Maybe FilePath @@ -218,7 +223,7 @@ dumpConfigOptions = hsubparser $ info parser $ progDesc "Dump default configuration into a file." where - parser = DumpConfig <$> repoTypeOption <*> outputOption + parser = DumpConfig <$> repoTypeOption <*> mode repoTypeOption = option repoTypeReadM $ @@ -231,6 +236,22 @@ dumpConfigOptions = hsubparser $ Case insensitive. |] + mode = + stdoutMode <|> fileMode + + fileMode = + DCMFile <$> forceMode <*> outputOption + + stdoutMode = + flag' DCMStdout $ + long "stdout" <> + help "Write the config file to stdout." + + forceMode = + switch $ + long "force" <> + help "Overwrite the config file if it already exists." + outputOption = filepathOption $ short 'o' <> diff --git a/tests/golden/check-dump-config/.config.yaml b/tests/golden/check-dump-config/.config.yaml new file mode 100644 index 00000000..914f5f94 --- /dev/null +++ b/tests/golden/check-dump-config/.config.yaml @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2022 Serokell +# +# SPDX-License-Identifier: MPL-2.0 diff --git a/tests/golden/check-dump-config/.xrefcheck.yaml b/tests/golden/check-dump-config/.xrefcheck.yaml new file mode 100644 index 00000000..914f5f94 --- /dev/null +++ b/tests/golden/check-dump-config/.xrefcheck.yaml @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2022 Serokell +# +# SPDX-License-Identifier: MPL-2.0 diff --git a/tests/golden/check-dump-config/check-dump-config.bats b/tests/golden/check-dump-config/check-dump-config.bats new file mode 100644 index 00000000..f2d09ab9 --- /dev/null +++ b/tests/golden/check-dump-config/check-dump-config.bats @@ -0,0 +1,75 @@ +#!/usr/bin/env bats + +# SPDX-FileCopyrightText: 2022 Serokell +# +# SPDX-License-Identifier: MPL-2.0 + +load '../helpers/bats-support/load' +load '../helpers/bats-assert/load' +load '../helpers/bats-file/load' +load '../helpers' + + +@test "Dump config to stdout" { + to_temp xrefcheck dump-config --stdout -t GitHub + + assert_diff ../../configs/github-config.yaml +} + +@test "Dump config to existent default file error" { + run xrefcheck dump-config -t GitHub + + assert_failure + + assert_output "Output file exists. Use --force to overwrite." +} + +@test "Dump config to existent file error" { + run xrefcheck dump-config -o .config.yaml -t GitHub + + assert_failure + + assert_output "Output file exists. Use --force to overwrite." +} + +@test "Dump config to non existent default file" { + cd $TEST_TEMP_DIR + + run xrefcheck dump-config -t GitHub + + assert_success + + assert_exists .xrefcheck.yaml +} + +@test "Dump config to non existent file" { + cd $TEST_TEMP_DIR + + run xrefcheck dump-config -o .config.yaml -t GitHub + + assert_success + + assert_exists .config.yaml +} + +@test "Dump config to existent default file with force" { + cp .xrefcheck.yaml $TEST_TEMP_DIR + cd $TEST_TEMP_DIR + + run xrefcheck dump-config -t GitHub --force + + assert_success + + assert_exists .xrefcheck.yaml +} + +@test "Dump config to existent file with force" { + cp .config.yaml $TEST_TEMP_DIR + cd $TEST_TEMP_DIR + + run xrefcheck dump-config -o .config.yaml -t GitHub --force + + assert_success + + assert_exists .config.yaml +} From 1c44c1c43ef70a95289dd288740765fb856e6548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Enr=C3=ADquez?= Date: Thu, 22 Dec 2022 21:17:09 +0100 Subject: [PATCH 2/3] [Style] Sort some package.yaml arrays Problem: some arrays from the package.yaml file seemed to be almost alphabetically sorted, but not completely. Solution: sort the default-extensions and dependencies arrays from the package.yaml file. --- package.yaml | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/package.yaml b/package.yaml index 2427c59e..8df1d052 100644 --- a/package.yaml +++ b/package.yaml @@ -46,11 +46,11 @@ default-extensions: - StandaloneDeriving - TemplateHaskell - TupleSections + - TypeApplications - TypeFamilies + - TypeOperators - UndecidableInstances - ViewPatterns - - TypeApplications - - TypeOperators ghc-options: - -Weverything @@ -85,8 +85,8 @@ library: - aeson-casing - async - bytestring - - containers - cmark-gfm >= 0.2.5 + - containers - directory - dlist - filepath @@ -96,14 +96,17 @@ library: - http-client - http-types - lens - - pretty-terminal - modern-uri - mtl + - nyan-interpolation - o-clock - optparse-applicative + - pretty-terminal - process + - reflection - regex-tdfa - req + - safe-exceptions - tagsoup - text - text-metrics @@ -112,9 +115,6 @@ library: - universum - uri-bytestring - yaml - - reflection - - nyan-interpolation - - safe-exceptions executables: xrefcheck: @@ -128,11 +128,11 @@ executables: - -with-rtsopts=-N - -O2 dependencies: - - xrefcheck - - universum + - code-page - directory + - universum - with-utf8 - - code-page + - xrefcheck tests: xrefcheck-tests: @@ -143,24 +143,24 @@ tests: - Paths_xrefcheck dependencies: - case-insensitive - - containers - cmark-gfm - - firefly - - xrefcheck + - containers - directory + - firefly - http-types + - modern-uri + - nyan-interpolation - o-clock + - reflection - regex-tdfa - tasty - tasty-hunit - tasty-quickcheck - time - universum - - modern-uri - uri-bytestring + - xrefcheck - yaml - - reflection - - nyan-interpolation ftp-tests: main: Main.hs @@ -173,5 +173,5 @@ tests: - tagged - tasty - tasty-hunit - - xrefcheck - universum + - xrefcheck From c9486e7ac65bbf10e31ad95277c5eb8a01591873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Enr=C3=ADquez?= Date: Fri, 23 Dec 2022 15:12:15 +0100 Subject: [PATCH 3/3] [Chore] Update comment and error message --- tests/Test/Xrefcheck/ConfigSpec.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Test/Xrefcheck/ConfigSpec.hs b/tests/Test/Xrefcheck/ConfigSpec.hs index e2892b12..d371ff83 100644 --- a/tests/Test/Xrefcheck/ConfigSpec.hs +++ b/tests/Test/Xrefcheck/ConfigSpec.hs @@ -35,14 +35,14 @@ test_config = , testGroup "Filled default config matches the expected format" -- The config we match against can be regenerated with - -- stack exec xrefcheck -- dump-config -t GitHub -o tests/configs/github-config.yaml + -- stack exec xrefcheck -- dump-config -t GitHub -o tests/configs/github-config.yaml --force [ testCase "Config matches" $ do config <- readFile "tests/configs/github-config.yaml" when (config /= defConfigText GitHub) $ assertFailure $ toString $ unwords [ "Config does not match the expected format." , "Run" - , "`stack exec xrefcheck -- dump-config -t GitHub -o tests/configs/github-config.yaml`" + , "`stack exec xrefcheck -- dump-config -t GitHub -o tests/configs/github-config.yaml --force`" , "and verify changes" ] ]