File: common.rs

package info (click to toggle)
lintian-brush 0.166
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,828 kB
  • sloc: makefile: 877; xml: 119; python: 80; sh: 69; javascript: 3
file content (217 lines) | stat: -rw-r--r-- 6,533 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/// Common test helpers for debianize tests
use breezyshim::controldir::ControlDirFormat;
use breezyshim::tree::{MutableTree, Tree};
use breezyshim::workingtree::{GenericWorkingTree, WorkingTree};
use debianize::{DebianizePreferences, SessionPreferences};
use std::path::Path;
use tempfile::TempDir;

/// Initialize a git repository with a working tree at the given path
pub fn init_working_tree(path: &Path) -> GenericWorkingTree {
    breezyshim::init();

    let format = ControlDirFormat::default();
    let transport =
        breezyshim::transport::get_transport(&url::Url::from_file_path(path).unwrap(), None)
            .unwrap();

    let controldir = format.initialize_on_transport(&transport).unwrap();
    controldir.create_repository(None).unwrap();
    controldir.create_branch(None).unwrap();
    controldir.create_workingtree().unwrap()
}

/// Create a simple Python package in the working tree
pub fn create_simple_python_package(
    wt: &GenericWorkingTree,
    name: &str,
    version: &str,
    dependencies: &[&str],
) {
    let module_name = name.replace('-', "_");

    // Create setup.py
    let deps_str = if dependencies.is_empty() {
        String::new()
    } else {
        format!(
            "\n    install_requires=[{}],",
            dependencies
                .iter()
                .map(|d| format!("\"{}\"", d))
                .collect::<Vec<_>>()
                .join(", ")
        )
    };

    let setup_py = format!(
        r#"#!/usr/bin/env python3
from setuptools import setup, find_packages

setup(
    name="{}",
    version="{}",
    author="Test Author",
    author_email="test@example.com",
    description="Test package for {}",
    packages=find_packages(),
    python_requires=">=3.8",{}
)
"#,
        name, version, name, deps_str
    );

    wt.put_file_bytes_non_atomic(Path::new("setup.py"), setup_py.as_bytes())
        .unwrap();

    // Create package directory
    wt.mkdir(Path::new(&module_name)).unwrap();

    // Create __init__.py
    let init_py = format!(
        r#""""Test package {}"""

__version__ = "{}"

def hello():
    return "Hello from {}"
"#,
        name, version, name
    );

    wt.put_file_bytes_non_atomic(
        Path::new(&format!("{}/__init__.py", module_name)),
        init_py.as_bytes(),
    )
    .unwrap();

    // Create README
    let readme = format!("# {}\n\nTest package version {}\n", name, version);
    wt.put_file_bytes_non_atomic(Path::new("README.md"), readme.as_bytes())
        .unwrap();

    // Add all files
    wt.add(&[
        Path::new("setup.py"),
        Path::new(&module_name),
        Path::new(&format!("{}/__init__.py", module_name)),
        Path::new("README.md"),
    ])
    .unwrap();

    // Commit
    wt.build_commit()
        .message(&format!("Initial commit for {}", name))
        .commit()
        .unwrap();
}

/// Create default debianize preferences for testing
pub fn default_test_preferences() -> DebianizePreferences {
    DebianizePreferences {
        use_inotify: Some(false),
        diligence: 0,
        trust: true,
        check: false,
        net_access: false,
        force_subprocess: false,
        force_new_directory: false,
        compat_release: Some("bookworm".to_string()),
        minimum_certainty: debian_analyzer::Certainty::Confident,
        consult_external_directory: false,
        verbose: false,
        session: SessionPreferences::default_isolated(),
        create_dist: None,
        committer: Some("Test User <test@example.com>".to_string()),
        upstream_version_kind: breezyshim::debian::VersionKind::Auto,
        debian_revision: "1".to_string(),
        team: None,
        author: Some("Test Packager <packager@example.com>".to_string()),
        compat_level: None,
        check_wnpp: false,
        run_fixers: false,
    }
}

/// Assert that basic debian files exist in the working tree
pub fn assert_debian_files_exist(wt: &GenericWorkingTree) {
    assert!(
        wt.has_filename(Path::new("debian/control")),
        "debian/control should exist"
    );
    assert!(
        wt.has_filename(Path::new("debian/rules")),
        "debian/rules should exist"
    );
    assert!(
        wt.has_filename(Path::new("debian/changelog")),
        "debian/changelog should exist"
    );
    assert!(
        wt.has_filename(Path::new("debian/source/format")),
        "debian/source/format should exist"
    );
}

/// Read and clean control file content (removes VCS fields)
pub fn read_cleaned_control(path: &Path) -> String {
    let content = std::fs::read_to_string(path.join("debian/control")).unwrap();
    content
        .lines()
        .filter(|line| !line.starts_with("Vcs-"))
        .collect::<Vec<_>>()
        .join("\n")
        + "\n"
}

/// Create a test repository with a simple Python package
pub fn create_test_python_repo(
    temp_dir: &TempDir,
    name: &str,
) -> (std::path::PathBuf, GenericWorkingTree) {
    let repo_path = temp_dir.path().join(name);
    std::fs::create_dir_all(&repo_path).unwrap();
    let wt = init_working_tree(&repo_path);
    (repo_path, wt)
}

pub(crate) struct DebianImageCached {
    old_env: Option<String>,
}

impl DebianImageCached {
    pub(crate) fn new() -> Result<Self, ognibuild::session::Error> {
        if let Ok(tarball_path) = std::env::var("OGNIBUILD_DEBIAN_TEST_TARBALL") {
            Ok(DebianImageCached {
                old_env: Some(tarball_path),
            })
        } else if let Ok(tarball_path) =
            ognibuild::session::unshare::cached_debian_tarball_path("sid")
        {
            if tarball_path.exists() {
                let old_env = std::env::var("OGNIBUILD_DEBIAN_TEST_TARBALL").ok();
                std::env::set_var("OGNIBUILD_DEBIAN_TEST_TARBALL", &tarball_path);
                Ok(DebianImageCached { old_env })
            } else {
                eprintln!("Cached Debian tarball does not exist at {:?}", tarball_path);
                Err(ognibuild::session::Error::ImageError(
                    ognibuild::session::ImageError::NoCachedImage,
                ))
            }
        } else {
            Err(ognibuild::session::Error::ImageError(
                ognibuild::session::ImageError::NoCachedImage,
            ))
        }
    }
}

impl Drop for DebianImageCached {
    fn drop(&mut self) {
        if let Some(old_env) = &self.old_env {
            std::env::set_var("OGNIBUILD_DEBIAN_TEST_TARBALL", old_env);
        } else {
            std::env::remove_var("OGNIBUILD_DEBIAN_TEST_TARBALL");
        }
    }
}