File: test_workspace.rs

package info (click to toggle)
jujutsu 0.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,740 kB
  • sloc: sh: 283; makefile: 34
file content (164 lines) | stat: -rw-r--r-- 5,484 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2021 The Jujutsu Authors
//
// 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::thread;

use assert_matches::assert_matches;
use jj_lib::ref_name::WorkspaceNameBuf;
use jj_lib::repo::Repo as _;
use jj_lib::workspace::Workspace;
use jj_lib::workspace::WorkspaceLoadError;
use jj_lib::workspace::default_working_copy_factories;
use jj_lib::workspace::default_working_copy_factory;
use testutils::TestEnvironment;
use testutils::TestWorkspace;

#[test]
fn test_load_bad_path() {
    let settings = testutils::user_settings();
    let test_env = TestEnvironment::init();
    let workspace_root = test_env.root().to_owned();
    // We haven't created a repo in the workspace_root, so it should fail to load.
    let result = Workspace::load(
        &settings,
        &workspace_root,
        &test_env.default_store_factories(),
        &default_working_copy_factories(),
    );
    assert_matches!(
        result.err(),
        Some(WorkspaceLoadError::NoWorkspaceHere(root)) if root == workspace_root
    );
}

#[test]
fn test_init_additional_workspace() {
    let settings = testutils::user_settings();
    let test_workspace = TestWorkspace::init_with_settings(&settings);
    let workspace = &test_workspace.workspace;

    let ws2_name = WorkspaceNameBuf::from("ws2");
    let ws2_root = test_workspace.root_dir().join("ws2_root");
    std::fs::create_dir(&ws2_root).unwrap();
    let (ws2, repo) = Workspace::init_workspace_with_existing_repo(
        &ws2_root,
        test_workspace.repo_path(),
        &test_workspace.repo,
        &*default_working_copy_factory(),
        ws2_name.clone(),
    )
    .unwrap();
    let wc_commit_id = repo.view().get_wc_commit_id(&ws2_name);
    assert_ne!(wc_commit_id, None);
    let wc_commit_id = wc_commit_id.unwrap();
    let wc_commit = repo.store().get_commit(wc_commit_id).unwrap();
    assert_eq!(
        wc_commit.parent_ids(),
        vec![repo.store().root_commit_id().clone()]
    );
    assert_eq!(ws2.workspace_name(), &ws2_name);
    assert_eq!(
        *ws2.repo_path(),
        dunce::canonicalize(workspace.repo_path()).unwrap()
    );
    assert_eq!(
        *ws2.workspace_root(),
        dunce::canonicalize(&ws2_root).unwrap()
    );
    let same_workspace = Workspace::load(
        &settings,
        &ws2_root,
        &test_workspace.env.default_store_factories(),
        &default_working_copy_factories(),
    );
    assert!(same_workspace.is_ok());
    let same_workspace = same_workspace.unwrap();
    assert_eq!(same_workspace.workspace_name(), &ws2_name);
    assert_eq!(
        *same_workspace.repo_path(),
        dunce::canonicalize(workspace.repo_path()).unwrap()
    );
    assert_eq!(same_workspace.workspace_root(), ws2.workspace_root());
}

#[cfg(unix)]
#[test]
fn test_init_additional_workspace_non_utf8_path() {
    use std::ffi::OsStr;
    use std::os::unix::ffi::OsStrExt as _;

    let settings = testutils::user_settings();
    let test_env = TestEnvironment::init();

    if testutils::check_strict_utf8_fs(test_env.root()) {
        eprintln!(
            "Skipping test \"test_init_additional_workspace_non_utf8_path\" due to strict UTF-8 \
             filesystem for path {:?}",
            test_env.root()
        );
        return;
    }

    let ws1_root = test_env.root().join(OsStr::from_bytes(b"ws1_root\xe0"));
    std::fs::create_dir(&ws1_root).unwrap();
    let (ws1, repo) = Workspace::init_simple(&settings, &ws1_root).unwrap();

    let ws2_name = WorkspaceNameBuf::from("ws2");
    let ws2_root = test_env.root().join(OsStr::from_bytes(b"ws2_root\xe0"));
    std::fs::create_dir(&ws2_root).unwrap();
    let (ws2, _repo) = Workspace::init_workspace_with_existing_repo(
        &ws2_root,
        ws1.repo_path(),
        &repo,
        &*default_working_copy_factory(),
        ws2_name.clone(),
    )
    .unwrap();
    assert_eq!(ws2.workspace_name(), &ws2_name);
    assert_eq!(
        *ws2.repo_path(),
        dunce::canonicalize(ws1.repo_path()).unwrap()
    );
    assert_eq!(
        *ws2.workspace_root(),
        dunce::canonicalize(&ws2_root).unwrap()
    );
    let same_workspace = Workspace::load(
        &settings,
        &ws2_root,
        &test_env.default_store_factories(),
        &default_working_copy_factories(),
    );
    let same_workspace = same_workspace.unwrap();
    assert_eq!(same_workspace.workspace_name(), &ws2_name);
    assert_eq!(
        *same_workspace.repo_path(),
        dunce::canonicalize(ws1.repo_path()).unwrap()
    );
    assert_eq!(same_workspace.workspace_root(), ws2.workspace_root());
}

/// Test cross-thread access to a workspace, which requires it to be Send
#[test]
fn test_sendable() {
    let test_workspace = TestWorkspace::init();
    let root = test_workspace.workspace.workspace_root().to_owned();

    thread::spawn(move || {
        let shared_workspace = test_workspace.workspace;
        assert_eq!(shared_workspace.workspace_root(), &root);
    })
    .join()
    .unwrap();
}