From 54285a9c56ff5054854f5891e8922dbdba6abbb8 Mon Sep 17 00:00:00 2001
From: Martin von Zweigbergk <martinvonz@google.com>
Date: Tue, 28 Jun 2022 18:19:01 -0700
Subject: [PATCH] tests: add test of `jj checkout`

I'm about to change how `jj checkout` works w.r.t. open/closed commits
(and in particular by moving some logic from the library crate to the
CLI), so let's make sure we have some test coverage.
---
 tests/test_checkout.rs | 63 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 tests/test_checkout.rs

diff --git a/tests/test_checkout.rs b/tests/test_checkout.rs
new file mode 100644
index 000000000..7c58c376f
--- /dev/null
+++ b/tests/test_checkout.rs
@@ -0,0 +1,63 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+use std::path::Path;
+
+use crate::common::TestEnvironment;
+
+pub mod common;
+
+#[test]
+fn test_checkout() {
+    let test_env = TestEnvironment::default();
+    test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
+    let repo_path = test_env.env_root().join("repo");
+
+    test_env.jj_cmd_success(&repo_path, &["close", "-m", "closed"]);
+    test_env.jj_cmd_success(&repo_path, &["describe", "-m", "open"]);
+    test_env.jj_cmd_success(&repo_path, &["branch", "create", "open"]);
+
+    // Check out current commit
+    let stdout = test_env.jj_cmd_success(&repo_path, &["checkout", "@"]);
+    insta::assert_snapshot!(stdout, @r###"
+    Already on that commit
+    "###);
+    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
+    @ 169fa76981bcf302d1a96952bdf32a8da79ab084 open
+    o b4c967d9c9a9e8b523b0a9b52879b3337a3e67a9 closed
+    o 0000000000000000000000000000000000000000 (no description set)
+    "###);
+
+    // When checking out a closed commit, a new commit is created on top of it
+    test_env.jj_cmd_success(&repo_path, &["checkout", "@-"]);
+    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
+    @ 5a38be51f15b107b7c7e89c06c0ab626f1457128 (no description set)
+    | o 169fa76981bcf302d1a96952bdf32a8da79ab084 open
+    |/  
+    o b4c967d9c9a9e8b523b0a9b52879b3337a3e67a9 closed
+    o 0000000000000000000000000000000000000000 (no description set)
+    "###);
+
+    // When checking out an open commit, the specified commit is edited directly
+    test_env.jj_cmd_success(&repo_path, &["checkout", "open"]);
+    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
+    @ 169fa76981bcf302d1a96952bdf32a8da79ab084 open
+    o b4c967d9c9a9e8b523b0a9b52879b3337a3e67a9 closed
+    o 0000000000000000000000000000000000000000 (no description set)
+    "###);
+}
+
+fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
+    test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id " " description"#])
+}