Fix config file round trip

Commit
db00ee997e5935b43ad4e2b5e287b67f7dffd740
Author
Marius Peter <dev@marius-peter.com>
Author date
Committer
Marius Peter <dev@marius-peter.com>
Committer date
Changed files
lib/config.ml
index 497a084c..780c4d7a 100644..100644
@@ -18,9 +18,13 @@
18 18 }
19 19
20 20 let locate_config_file () =
21 Removed: match List.find_map Sys.getenv_opt [ "OGIT_CONFIG"; "XDG_CONFIG_HOME" ] with
21 Added: match Sys.getenv_opt "OGIT_CONFIG" with
22 22 | Some file -> file
23 Removed: | None -> "/etc/ogit/config.toml"
23 Added: | None -> (
24 Added: match Sys.getenv_opt "XDG_CONFIG_HOME" with
25 Added: | Some config_home ->
26 Added: Filename.concat (Filename.concat config_home "ogit") "config.toml"
27 Added: | None -> "/etc/ogit/config.toml")
24 28
25 29 let config_file = locate_config_file ()
26 30
@@ -63,7 +67,7 @@
63 67 let* git_project_root = find_string "git_project_root" in
64 68 let* user = find_string "user" in
65 69 let* default_branch = find_string "default_branch" in
66 Removed: let* commits_max_displayed = find_int "max_commits_displayed" in
70 Added: let* commits_max_displayed = find_int "commits_max_displayed" in
67 71 Ok { git_project_root; user; default_branch; commits_max_displayed }
68 72 with _ ->
69 73 prerr_endline "[config.ml] Falling back to default config.";
test/dune
index e4a34b1a..1bb5f7a5 100644..100644
@@ -1,3 +1,3 @@
1 1 (test
2 2 (name test_ogit)
3 Removed: (libraries ogit))
3 Added: (libraries ogit unix))
test/test_ogit.ml
index a82c73a2..9f26a436 100644..100644
@@ -1,3 +1,29 @@
1 Added: let test_config_round_trip () =
2 Added: let file = Filename.temp_file "ogit" ".toml" in
3 Added: let config =
4 Added: Ogit.Config.
5 Added: {
6 Added: user = "alice";
7 Added: default_branch = "main";
8 Added: git_project_root = "/srv/git";
9 Added: commits_max_displayed = 25;
10 Added: }
11 Added: in
12 Added: Ogit.Config.write_file ~file (Ogit.Config.to_table config);
13 Added: match Ogit.Config.read_file ~file () with
14 Added: | Ok config' -> assert (config' = config)
15 Added: | Error message -> failwith message
16 Added:
17 Added: let test_config_location () =
18 Added: if Sys.getenv_opt "OGIT_CONFIG" = None then (
19 Added: Unix.putenv "XDG_CONFIG_HOME" "/tmp/xdg-config";
20 Added: assert (
21 Added: Ogit.Config.locate_config_file ()
22 Added: = Filename.concat (Filename.concat "/tmp/xdg-config" "ogit")
23 Added: "config.toml"));
24 Added: Unix.putenv "OGIT_CONFIG" "/tmp/custom-ogit.toml";
25 Added: assert (Ogit.Config.locate_config_file () = "/tmp/custom-ogit.toml")
26 Added:
1 27 let () =
2 28 assert (Ogit.Resolvers.is_valid_repo_name "project.git");
3 29 assert (Ogit.Resolvers.is_valid_repo_name "project");
@@ -17,4 +43,6 @@
17 43 assert (Ogit.Resolvers.Reference.branch_name "HEAD" = None);
18 44 assert (
19 45 Ogit.Resolvers.Reference.tag_name "refs/tags/v1.0.0" = Some "v1.0.0");
20 Removed: assert (Ogit.Resolvers.Reference.tag_name "refs/heads/v1.0.0" = None)
46 Added: assert (Ogit.Resolvers.Reference.tag_name "refs/heads/v1.0.0" = None);
47 Added: test_config_round_trip ();
48 Added: test_config_location ()