Skip to content

Commit c059f0a

Browse files
committed
Got test of adding titles to settings schemas
1 parent 7b75458 commit c059f0a

File tree

8 files changed

+101
-31
lines changed

8 files changed

+101
-31
lines changed

modules/kernels/bash/default.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
, settingsSchema
1313
}:
1414

15-
with { inherit (settings) attrs extensions; };
15+
with { inherit (settings.interface) attrs extensions; };
1616

1717
let
1818
kernelName = "bash";

modules/kernels/bash/module.nix

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ config, options, lib, pkgs, nixosOptionsToSettingsSchema, ... }:
1+
{ config, options, lib, pkgs, nixosOptionsToSettingsSchema, boilerplate, ... }:
22

33
with lib;
44

@@ -12,12 +12,15 @@ with lib;
1212
visible = false;
1313
};
1414

15-
attrs = mkOption {
15+
interface.attrs = mkOption {
16+
example = boilerplate.attrsTitle;
17+
description = boilerplate.attrsDescription;
1618
type = types.listOf types.str;
1719
default = ["bash"];
1820
};
19-
20-
extensions = mkOption {
21+
interface.extensions = mkOption {
22+
example = boilerplate.extensionsTitle;
23+
description = boilerplate.extensionsDescription;
2124
type = types.listOf types.str;
2225
default = ["sh" "bash"];
2326
};

modules/kernels/python/module.nix

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ config, options, lib, pkgs, nixosOptionsToSettingsSchema, ... }:
1+
{ config, options, lib, pkgs, nixosOptionsToSettingsSchema, boilerplate, ... }:
22

33
with lib;
44

@@ -20,14 +20,14 @@ let
2020
python3Package = packageOption;
2121

2222
interface.attrs = mkOption {
23-
example = "Notebook attributes";
24-
description = "Notebook cells that have these attributes will match this kernel, allowing it to run the code.";
23+
example = boilerplate.attrsTitle;
24+
description = boilerplate.attrsDescription;
2525
type = types.listOf types.str;
2626
default = ["python"];
2727
};
2828
interface.extensions = mkOption {
29-
example = "File extensions";
30-
description = "Files with these extensions will match against this kernel, allowing you to run the code as if it were a Jupyter cell.";
29+
example = boilerplate.extensionsTitle;
30+
description = boilerplate.extensionsDescription;
3131
type = types.listOf types.str;
3232
default = ["py"];
3333
};

nix/evaluate-config.nix

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ config:
88
lib.evalModules {
99
specialArgs = {
1010
nixosOptionsToSettingsSchema = pkgsStable.callPackage ../modules/base/nixos-options-to-settings-schema.nix {};
11+
boilerplate = {
12+
attrsTitle = "Notebook attributes";
13+
attrsDescription = "Notebook cells that have these attributes will match this kernel, allowing it to run the code.";
14+
15+
extensionsTitle = "File extensions";
16+
extensionsDescription = "Files with these extensions will match against this kernel, allowing you to run the code as if it were a Jupyter cell.";
17+
};
1118
};
1219
modules = [
1320
../modules/base.nix
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}
2+
3+
module Spec.Tests.SettingsSchemas (tests) where
4+
5+
import Control.Monad.IO.Class
6+
import Data.Aeson as A
7+
import qualified Data.Map as M
8+
import Data.Maybe
9+
import Data.String.Interpolate
10+
import Data.Text
11+
import Test.Sandwich as Sandwich
12+
import TestLib.JupyterRunnerContext
13+
import TestLib.TestBuilding
14+
import TestLib.Types
15+
import TestLib.Util
16+
17+
18+
type PackageName = Text
19+
type TargetName = Text
20+
type AllSettingsSchemas = M.Map PackageName (M.Map TargetName A.Object)
21+
22+
tests :: SimpleSpec
23+
tests = describe "Settings schemas" $ do
24+
it "all settings schema items have required fields" $ do
25+
allSettingsSchemasFile <- testBuildUsingFlake ".#allSettingsSchemas"
26+
info [i|Got file: #{allSettingsSchemasFile}|]
27+
28+
Right (byPackage :: AllSettingsSchemas) <- liftIO $ A.eitherDecodeFileStrict allSettingsSchemasFile
29+
30+
itemsLackingField byPackage "defaultValue" `shouldBe` []
31+
32+
info [i|Num lacking title: #{Prelude.length (itemsLackingField byPackage "title")}|]
33+
itemsLackingField byPackage "title" `shouldBe` []
34+
35+
itemsLackingField :: AllSettingsSchemas -> Text -> [Text]
36+
itemsLackingField byPackage field = catMaybes [
37+
classify packageName targetName item
38+
| (packageName, m) <- M.toList byPackage
39+
, (targetName, item) <- M.toList m
40+
]
41+
where
42+
classify packageName targetName item = case item of
43+
(aesonLookup field -> Just _) -> Nothing
44+
_ -> Just ([i|#{packageName}.#{targetName}|] :: Text)
45+
46+
main :: IO ()
47+
main = runSandwichWithCommandLineArgs Sandwich.defaultOptions $
48+
introduceBootstrapNixpkgs $
49+
tests

tests/src/TestLib/TestBuilding.hs

+27-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ module TestLib.TestBuilding where
55
import Conduit as C
66
import Control.Monad.Logger
77
import Control.Monad.Reader
8+
import Data.Aeson as A
9+
import Data.ByteString.Lazy.Char8 as BL8
810
import Data.Function
911
import qualified Data.List as L
12+
import Data.String.Interpolate
13+
import Data.Text as T
1014
import GHC.Stack
1115
import System.Exit
1216
import System.FilePath
1317
import Test.Sandwich
18+
import TestLib.JupyterRunnerContext
1419
import TestLib.Types
1520
import TestLib.Util
1621
import UnliftIO.Directory
@@ -21,22 +26,37 @@ import UnliftIO.Process
2126
testBuild :: (
2227
HasCallStack, MonadUnliftIO m, MonadLogger m, MonadReader context m
2328
, HasBaseContext context, HasBootstrapNixpkgs context
24-
) => String -> m ()
29+
) => String -> m FilePath
2530
testBuild = testBuild' LevelDebug
2631

2732
testBuild' :: (
2833
HasCallStack, MonadUnliftIO m, MonadLogger m, MonadReader context m
2934
, HasBaseContext context, HasBootstrapNixpkgs context
30-
) => LogLevel -> String -> m ()
35+
) => LogLevel -> String -> m FilePath
3136
testBuild' logLevel expr = do
3237
rootDir <- findFirstParentMatching (\x -> doesPathExist (x </> ".git"))
3338
env <- getEnvWithNixPath
39+
let cp = (proc "nix-build" [".", "-A", expr, "--no-out-link"]) {
40+
cwd = Just rootDir
41+
, env = Just env
42+
}
43+
(T.unpack . T.strip . T.pack) <$> (readCreateProcessWithLogging' logLevel cp "")
3444

35-
p <- createProcessWithLogging' logLevel $ (proc "nix-build" [".", "-A", expr, "--no-out-link"]) {
36-
cwd = Just rootDir
37-
, env = Just env
38-
}
39-
waitForProcess p >>= (`shouldBe` ExitSuccess)
45+
testBuildUsingFlake :: (
46+
HasCallStack, MonadUnliftIO m, MonadLogger m, MonadReader context m
47+
, HasBaseContext context, HasBootstrapNixpkgs context
48+
) => String -> m FilePath
49+
testBuildUsingFlake expr = do
50+
rootDir <- findFirstParentMatching (\x -> doesPathExist (x </> ".git"))
51+
let cp = (proc "nix" ["build", expr, "--no-link", "--json"]) {
52+
cwd = Just rootDir
53+
, std_err = CreatePipe
54+
}
55+
out <- readCreateProcessWithLogging cp ""
56+
case parseNixBuildJson <$> (A.eitherDecode (BL8.pack out)) of
57+
Left err -> expectationFailure [i|Failed to parse bin paths: #{err}. JSON: #{out}|]
58+
Right Nothing -> expectationFailure [i|Didn't find Nix output in: #{out}|]
59+
Right (Just output) -> pure $ T.unpack output
4060

4161
testEval :: (
4262
HasCallStack, MonadUnliftIO m, MonadLogger m, MonadReader context m

tests/src/TestLib/TestSearchers.hs

+4-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module TestLib.TestSearchers where
44

55
import Conduit as C
6+
import Control.Monad
67
import Control.Monad.Catch (MonadMask)
78
import Control.Monad.Logger
89
import Control.Monad.Reader
@@ -17,8 +18,6 @@ import System.FilePath
1718
import Test.Sandwich
1819
import TestLib.TestBuilding
1920
import TestLib.Types
20-
import TestLib.Util
21-
import UnliftIO.Directory
2221
import UnliftIO.IO
2322
import UnliftIO.Process
2423

@@ -30,7 +29,7 @@ testKernelSearchersBuild :: (
3029
, HasBaseContext context, HasBootstrapNixpkgs context
3130
) => Text -> SpecFree context m ()
3231
testKernelSearchersBuild kernel = it [i|#{kernel}: package searchers build|] $ do
33-
testBuild [i|kernels."#{kernel}".packageSearch|]
32+
void $ testBuild [i|kernels."#{kernel}".packageSearch|]
3433

3534
testHasExpectedFields :: (
3635
MonadIO m, MonadMask m, MonadUnliftIO m, MonadBaseControl IO m
@@ -75,20 +74,11 @@ searcherResults :: (
7574
, HasBaseContext context, HasBootstrapNixpkgs context
7675
) => String -> m [A.Object]
7776
searcherResults expr = do
78-
rootDir <- findFirstParentMatching (\x -> doesPathExist (x </> ".git"))
79-
80-
env <- getEnvWithNixPath
81-
let cp = (proc "nix-build" [".", "-A", expr, "--no-out-link"]) {
82-
cwd = Just rootDir
83-
, std_err = CreatePipe
84-
, env = Just env
85-
}
86-
built <- (T.unpack . T.strip . T.pack) <$> (readCreateProcessWithLogging cp "")
77+
built <- testBuild expr
8778
info [i|Got built searcher: #{built}|]
8879

8980
let cp' = (proc (built </> "bin" </> "searcher") []) {
90-
cwd = Just rootDir
91-
, std_in = CreatePipe
81+
std_in = CreatePipe
9282
, std_out = CreatePipe
9383
, std_err = CreatePipe
9484
, create_group = True

tests/tests.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ executable tests
111111
Spec.Tests.Rust.Diagnostics
112112
Spec.Tests.Rust.Hovers
113113
Spec.Tests.Searchers
114+
Spec.Tests.SettingsSchemas
114115
Spec.Tests.Shells.Zsh
115116
Spec.Tests.Spellchecker
116117
Paths_tests

0 commit comments

Comments
 (0)