jujube_lib/
testutils.rs

1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fs;
16use std::fs::OpenOptions;
17use std::io::Write;
18use std::sync::Arc;
19
20use tempfile::TempDir;
21
22use crate::commit_builder::CommitBuilder;
23use crate::repo::ReadonlyRepo;
24use crate::repo_path::{DirRepoPath, FileRepoPath};
25use crate::settings::UserSettings;
26use crate::store::{FileId, TreeId, TreeValue};
27use crate::store_wrapper::StoreWrapper;
28use crate::tree::Tree;
29use crate::tree_builder::TreeBuilder;
30
31pub fn user_settings() -> UserSettings {
32    let mut config = config::Config::new();
33    config.set("user.name", "Test User").unwrap();
34    config.set("user.email", "test.user@example.com").unwrap();
35    UserSettings::from_config(config)
36}
37
38pub fn init_repo(settings: &UserSettings, use_git: bool) -> (TempDir, Arc<ReadonlyRepo>) {
39    let temp_dir = tempfile::tempdir().unwrap();
40
41    let wc_path = temp_dir.path().join("repo");
42    fs::create_dir(&wc_path).unwrap();
43
44    let repo = if use_git {
45        let git_path = temp_dir.path().join("git-repo");
46        git2::Repository::init(&git_path).unwrap();
47        ReadonlyRepo::init_external_git(&settings, wc_path, git_path)
48    } else {
49        ReadonlyRepo::init_local(&settings, wc_path)
50    };
51
52    (temp_dir, repo)
53}
54
55pub fn write_file(store: &StoreWrapper, path: &FileRepoPath, contents: &str) -> FileId {
56    store.write_file(path, &mut contents.as_bytes()).unwrap()
57}
58
59pub fn write_normal_file(tree_builder: &mut TreeBuilder, path: &FileRepoPath, contents: &str) {
60    let id = write_file(tree_builder.repo(), path, contents);
61    tree_builder.set(
62        path.to_repo_path(),
63        TreeValue::Normal {
64            id,
65            executable: false,
66        },
67    );
68}
69
70pub fn write_executable_file(tree_builder: &mut TreeBuilder, path: &FileRepoPath, contents: &str) {
71    let id = write_file(tree_builder.repo(), path, contents);
72    tree_builder.set(
73        path.to_repo_path(),
74        TreeValue::Normal {
75            id,
76            executable: true,
77        },
78    );
79}
80
81pub fn write_symlink(tree_builder: &mut TreeBuilder, path: &FileRepoPath, target: &str) {
82    let id = tree_builder.repo().write_symlink(path, target).unwrap();
83    tree_builder.set(path.to_repo_path(), TreeValue::Symlink(id));
84}
85
86pub fn create_tree(repo: &ReadonlyRepo, path_contents: &[(&FileRepoPath, &str)]) -> Tree {
87    let store = repo.store();
88    let mut tree_builder = store.tree_builder(store.empty_tree_id().clone());
89    for (path, contents) in path_contents {
90        write_normal_file(&mut tree_builder, path, contents);
91    }
92    let id = tree_builder.write_tree();
93    store.get_tree(&DirRepoPath::root(), &id).unwrap()
94}
95
96#[must_use]
97pub fn create_random_tree(repo: &ReadonlyRepo) -> TreeId {
98    let mut tree_builder = repo
99        .store()
100        .tree_builder(repo.store().empty_tree_id().clone());
101    let number = rand::random::<u32>();
102    let path = FileRepoPath::from(format!("file{}", number).as_str());
103    write_normal_file(&mut tree_builder, &path, "contents");
104    tree_builder.write_tree()
105}
106
107#[must_use]
108pub fn create_random_commit(settings: &UserSettings, repo: &ReadonlyRepo) -> CommitBuilder {
109    let tree_id = create_random_tree(repo);
110    let number = rand::random::<u32>();
111    CommitBuilder::for_new_commit(settings, repo.store(), tree_id)
112        .set_description(format!("random commit {}", number))
113}
114
115pub fn write_working_copy_file(repo: &ReadonlyRepo, path: &FileRepoPath, contents: &str) {
116    let mut file = OpenOptions::new()
117        .write(true)
118        .create(true)
119        .truncate(true)
120        .open(repo.working_copy_path().join(path.to_internal_string()))
121        .unwrap();
122    file.write_all(contents.as_bytes()).unwrap();
123}