rustpython_unparser/
lib.rs

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
pub mod unparser;
mod utils;
pub use crate::unparser::Unparser;

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use rustpython_ast::text_size::TextRange;
    use rustpython_ast::Fold;
    use rustpython_ast::TextSize;
    use rustpython_parser::ast::Suite;
    use rustpython_parser::Parse;

    use std::fs;
    use std::io;
    use std::path::Path;
    use std::process::Command;

    struct RangesEraser {}

    impl Fold<TextRange> for RangesEraser {
        type TargetU = TextRange;

        type Error = std::convert::Infallible;

        type UserContext = TextRange;

        fn will_map_user(&mut self, _user: &TextRange) -> Self::UserContext {
            TextRange::new(TextSize::new(0), TextSize::new(0))
        }

        fn map_user(
            &mut self,
            _user: TextRange,
            start: Self::UserContext,
        ) -> Result<Self::TargetU, Self::Error> {
            Ok(start)
        }
    }

    fn run_tests_on_folders(source_folder: &str, results_folder: &str) -> io::Result<()> {
        for entry in fs::read_dir(results_folder)? {
            let entry = entry?;

            let entry_path = entry.path();
            if entry_path.is_file()
                && entry_path.file_name().is_some_and(|name| {
                    name.to_str()
                        .is_some_and(|inner_name| inner_name.ends_with(".py"))
                })
            {
                fs::remove_file(entry_path)?;
            }
        }

        for entry in fs::read_dir(source_folder)? {
            let entry = entry?;

            let entry_path = entry.path();

            if entry_path.is_file()
                && entry_path.file_name().is_some_and(|name| {
                    name.to_str()
                        .is_some_and(|inner_name| inner_name.ends_with(".py"))
                })
            {
                let file_content = fs::read_to_string(&entry_path)?;
                let entry_path_str = entry_path.to_str().unwrap();
                let mut unparser = Unparser::new();
                let stmts = Suite::parse(&file_content, entry_path_str).unwrap();
                for stmt in &stmts {
                    unparser.unparse_stmt(stmt);
                }
                let new_source = unparser.source;
                let old_file_name = entry_path.file_name().unwrap().to_str().unwrap();
                let new_file_name = old_file_name.replace(".py", "_unparsed.py");
                let new_entry_path_str = format!("{}/{}", results_folder, new_file_name);
                let new_entry_path = Path::new(&new_entry_path_str);
                fs::write(&new_entry_path, &new_source)?;
                let new_stmts =
                    Suite::parse(&new_source, new_entry_path.to_str().unwrap()).unwrap();
                // erase range information
                let mut eraser = RangesEraser {};
                let mut erased_new_stmts = Vec::new();
                for stmt in &new_stmts {
                    erased_new_stmts.push(eraser.fold_stmt(stmt.to_owned()).unwrap());
                }

                let mut erased_stmts = Vec::new();
                for stmt in &stmts {
                    erased_stmts.push(eraser.fold_stmt(stmt.to_owned()).unwrap());
                }

                for (stmt, new_stmt) in erased_stmts.iter().zip(erased_new_stmts.iter()) {
                    assert_eq!(stmt, new_stmt)
                }
            }
        }
        Ok(())
    }

    #[test]
    fn test_predefined_files() -> io::Result<()> {
        run_tests_on_folders("./test_files", "./test_files_unparsed")
    }
    #[test]
    #[ignore = "Fuzzy tests are unstable and should only be used to explore new test cases"]
    fn test_fuzzy_files() -> io::Result<()> {
        let seed = rand::random::<usize>();

        for i in 0..10 {
            let file_name = format!("./fuzzy_test_files/fuzzy_test{}.py", i);

            let file_content = Command::new(".venv/bin/python")
                .arg("-m")
                .arg("pysource_codegen")
                .arg("--seed")
                .arg(&seed.to_string())
                .output()
                .expect("failed to execute process");
            fs::write(&file_name, &file_content.stdout)?;
        }

        run_tests_on_folders("./fuzzy_test_files", "./fuzzy_test_files_unparsed")?;

        Ok(())
    }
}