Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Commit

Permalink
Build script generates slow integration tests.
Browse files Browse the repository at this point in the history
`build.rs` generates a file `tests/test_generated.rs` which
contains unit tests to check that the Chawathe+ 96 algorithm can
generate a valid edit script for every pair of input files in
`tests/`. These tests take a long time to run, and so are
marked with `#[ignore]`. They can be run with:

`cargo test --no-fail-fast  -- --ignored --nocapture`

A number of failing cases found with this new test have been
added as ordinary test cases.

Some code common to the generated integration tests and other
files has been factored into `tests/common.rs`.
  • Loading branch information
Sarah Mount committed Mar 2, 2018
1 parent 7ec9cf0 commit ce44009
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
target/
**/*.rs.bk
Cargo.lock
tests/test_generated.rs
6 changes: 5 additions & 1 deletion Cargo.toml
Expand Up @@ -7,6 +7,7 @@ repository = "https://github.com/softdevteam/diffract"
homepage = "https://github.com/softdevteam/diffract.git"
license = "UPL"
description = "Generate source code diffs based on ASTs."
build = "build.rs"

[[bin]]
doc = false
Expand All @@ -16,10 +17,13 @@ name = "diffract"
name = "diffract"
path = "src/lib/mod.rs"

[build-dependencies]
glob = "0.2.11"

[dependencies]
docopt = "0.7.0"
dot = "0.1.2"
env_logger = "0.4.2"
docopt = "0.7.0"
log = "0.3.7"
rustc-serialize = "0.3"
term = "0.4.5"
Expand Down
123 changes: 123 additions & 0 deletions build.rs
@@ -0,0 +1,123 @@
// Copyright (c) 2018 King's College London
// created by the Software Development Team <http://soft-dev.org/>
//
// The Universal Permissive License (UPL), Version 1.0
//
// Subject to the condition set forth below, permission is hereby granted to any
// person obtaining a copy of this software, associated documentation and/or
// data (collectively the "Software"), free of charge and under any and all
// copyright rights in the Software, and any and all patent rights owned or
// freely licensable by each licensor hereunder covering either (i) the
// unmodified Software as contributed to or provided by such licensor, or (ii)
// the Larger Works (as defined below), to deal in both
//
// (a) the Software, and
// (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
// if one is included with the Software (each a "Larger Work" to which the Software
// is contributed by such licensors),
//
// without restriction, including without limitation the rights to copy, create
// derivative works of, display, perform, and distribute the Software and make,
// use, sell, offer for sale, import, export, have made, and have sold the
// Software and the Larger Work(s), and to sublicense the foregoing rights on
// either these or other terms.
//
// This license is subject to the following condition: The above copyright
// notice and either this complete permission notice or at a minimum a reference
// to the UPL must be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Write out a file of integration tests which tests every pair of example
//! files in the `tests/` directory.

#![warn(missing_docs)]

extern crate glob;

use std::fs::File;
use std::io::Write;
use std::path::Path;

use glob::glob;

/// Directory containing example files for testing and to which the generated
/// tests should be written.
const DIR: &str = "tests/";

/// Generate a file of integration tests which run a diff (with both Null and
/// Myers matchers) on every pair of test files in the `tests/` directory.
/// Include diffs of files against themselves. This test takes a long time to
/// run (well over 5 minutes) and so should not be run on CI infrastructure such
/// as Travis.
///
/// Run with: `cargo test --no-fail-fast -- --ignored --nocapture`
fn main() {
// Glob all example files in the `tests/` directory.
// Filter out files which have been seeded with deliberate lexical or
// syntactic errors.
let test_files: Vec<String> =
glob(&format!("{}*.calc", DIR)).expect("Failed to read glob pattern")
.into_iter()
.chain(glob(&format!("{}*.java", DIR))
.expect("Failed to read glob pattern")
.into_iter())
.chain(glob(&format!("{}*.txt", DIR))
.expect("Failed to read glob pattern")
.into_iter())
.map(|res| res.unwrap()
.to_str()
.unwrap()
.to_string())
.filter(|name| !name.contains("_err"))
.collect::<Vec<String>>();

let mut outfile = File::create(Path::new(&DIR).join("test_generated.rs")).unwrap();
write!(
outfile,
r#"
extern crate diffract;
use diffract::myers_matcher::MyersConfig;
use diffract::null_matcher::NullConfig;
mod common;
use common::check_files;
"#
).unwrap();

for fname1 in test_files.iter() {
for fname2 in test_files.iter() {
// Remove directory and extension from `fname1`, `fname2`.
// We include the file extension in the test name because some files
// are intended to be parsed by different Yacc files (e.g.
// `Hello.java` and `Hello.txt`).
let name1_ext = fname1[DIR.len()..].split(".").collect::<Vec<&str>>();
let name2_ext = fname2[DIR.len()..].split(".").collect::<Vec<&str>>();
let name1 = format!("{}_{}", name1_ext[0], name1_ext[1]).to_lowercase();
let name2 = format!("{}_{}", name2_ext[0], name2_ext[1]).to_lowercase();
write!(outfile, r#"
#[test]
#[ignore]
fn test_globbed_{name1}_{name2}_null() {{
check_files("{fname1}", "{fname2}", Box::new(NullConfig::new()));
}}
#[test]
#[ignore]
fn test_globbed_{name1}_{name2}_myers() {{
check_files("{fname1}", "{fname2}", Box::new(MyersConfig::new()));
}}
"#, name1=name1, name2=name2, fname1=fname1, fname2=fname2).unwrap();
}
}
}
2 changes: 1 addition & 1 deletion rustfmt.toml
@@ -1,2 +1,2 @@
max_width = 120
max_width = 90
indent_style = "Visual"
5 changes: 5 additions & 0 deletions tests/Hello.txt
@@ -0,0 +1,5 @@
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
5 changes: 5 additions & 0 deletions tests/Test0.txt
@@ -0,0 +1,5 @@
public class Test {
public String foo(int i) {
if (i == 0) return "Foo!";
}
}
85 changes: 85 additions & 0 deletions tests/common.rs
@@ -0,0 +1,85 @@
// Copyright (c) 2018 King's College London
// created by the Software Development Team <http://soft-dev.org/>
//
// The Universal Permissive License (UPL), Version 1.0
//
// Subject to the condition set forth below, permission is hereby granted to any
// person obtaining a copy of this software, associated documentation and/or
// data (collectively the "Software"), free of charge and under any and all
// copyright rights in the Software, and any and all patent rights owned or
// freely licensable by each licensor hereunder covering either (i) the
// unmodified Software as contributed to or provided by such licensor, or (ii)
// the Larger Works (as defined below), to deal in both
//
// (a) the Software, and
// (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
// if one is included with the Software (each a "Larger Work" to which the Software
// is contributed by such licensors),
//
// without restriction, including without limitation the rights to copy, create
// derivative works of, display, perform, and distribute the Software and make,
// use, sell, offer for sale, import, export, have made, and have sold the
// Software and the Larger Work(s), and to sublicense the foregoing rights on
// either these or other terms.
//
// This license is subject to the following condition: The above copyright
// notice and either this complete permission notice or at a minimum a reference
// to the UPL must be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Code common to several integration tests.
#![warn(missing_docs)]

extern crate diffract;

use diffract::edit_script::{Chawathe96Config, EditScriptGenerator};
use diffract::matchers::MatchTrees;
use diffract::parser::{get_lexer, get_parser, parse_file};

/// Check that the Chawathe 1996 edit script generator correctly generates an
/// edit script for two files and a given matcher.
pub fn check_files(path1: &str, path2: &str, matcher: Box<MatchTrees<String>>) {
let ast_from = parse_file(path1, &get_lexer(path1), &get_parser(path1)).unwrap();
let ast_to = parse_file(path2, &get_lexer(path2), &get_parser(path2)).unwrap();
// Generate mappings between ASTs.
let store = matcher.match_trees(ast_from.clone(), ast_to.clone());
// Generate an edit script.
let gen_config: Box<EditScriptGenerator<String>> = Box::new(Chawathe96Config::new());
let edit_script_wrapped = gen_config.generate_script(&store);
assert!(edit_script_wrapped.is_ok(),
"Edit script generator failed to complete.");
// Get root node ids after the edit script has run (note that the root nodes
// may be altered by the edit script).
let root_from = store.from_arena.borrow().root().unwrap();
let root_to = store.to_arena.borrow().root().unwrap();
// Test 1: every reachable node in both ASTs should be mapped. The edit
// script generator should have turned the partial matching provided by the
// matcher into a total matching. N.B. deleted nodes in the to AST will be
// in the arena but should not be reachable from the root node.
let mut count_nodes = 0;
let mut count_mapped = 0;
for node in root_from.breadth_first_traversal(&store.from_arena.borrow()) {
count_nodes += 1;
assert!(store.contains_from(&node));
count_mapped += 1;
}
assert_eq!(count_nodes, count_mapped);
count_nodes = 0;
count_mapped = 0;
for node in root_to.breadth_first_traversal(&store.to_arena.borrow()) {
count_nodes += 1;
assert!(store.contains_to(&node));
count_mapped += 1;
}
assert_eq!(count_nodes, count_mapped,
"Final mapping not total for files: {} and {}.",
path1, path2);
}
80 changes: 41 additions & 39 deletions tests/test_edit_script.rs
Expand Up @@ -37,7 +37,6 @@

//! Integration tests for `edit_script` module.
//! All file paths are relative to the root of the repository.

extern crate diffract;

use diffract::ast::Arena;
Expand All @@ -47,44 +46,8 @@ use diffract::myers_matcher::MyersConfig;
use diffract::null_matcher::NullConfig;
use diffract::parser::{get_lexer, get_parser, parse_file};

fn check_files(path1: &str, path2: &str, matcher: Box<MatchTrees<String>>) {
// Lex and parse the input files.
let ast_from = parse_file(path1, &get_lexer(path1), &get_parser(path1)).unwrap();
let ast_to = parse_file(path2, &get_lexer(path2), &get_parser(path2)).unwrap();
// Generate mappings between ASTs.
let store = matcher.match_trees(ast_from.clone(), ast_to.clone());
// Generate an edit script.
let gen_config: Box<EditScriptGenerator<String>> = Box::new(Chawathe96Config::new());
let edit_script_wrapped = gen_config.generate_script(&store);
assert!(edit_script_wrapped.is_ok(),
"Edit script generator failed to complete.");
// Get root nodes after the edit script has run (note that the root nodes
// may be altered by the edit script.
let root_from = store.from_arena.borrow().root().unwrap();
let root_to = store.to_arena.borrow().root().unwrap();
// Test 1: every reachable node in both ASTs should be mapped. The edit
// script generator should have turned the partial matching provided by the
// matcher into a total matching. N.B. deleted nodes in the to AST will be
// in the arena but should not be reachable from the root node.
let mut count_nodes = 0;
let mut count_mapped = 0;
for node in root_from.breadth_first_traversal(&store.from_arena.borrow()) {
count_nodes += 1;
assert!(store.contains_from(&node));
count_mapped += 1;
}
assert_eq!(count_nodes, count_mapped);
count_nodes = 0;
count_mapped = 0;
for node in root_to.breadth_first_traversal(&store.to_arena.borrow()) {
count_nodes += 1;
assert!(store.contains_to(&node));
count_mapped += 1;
}
assert_eq!(count_nodes, count_mapped,
"Final mapping not total for files: {} and {}.",
path1, path2);
}
mod common;
use common::check_files;

#[test]
#[should_panic]
Expand Down Expand Up @@ -332,3 +295,42 @@ fn test_add_hello_both_null() {
"tests/Hello.java",
Box::new(NullConfig::new()))
}

// The following are regression tests. These combinations of files have
// previously triggered bugs found by running tests generated by build.rs.

#[test]
fn test_hello_wiki1_myers() {
check_files("tests/Hello.txt",
"tests/wiki1.txt",
Box::new(MyersConfig::new()))
}

#[test]
fn test_hello_test1_myers() {
check_files("tests/Hello.java",
"tests/Test0.java",
Box::new(MyersConfig::new()))
}

#[test]
#[ignore]
fn test_hello_test0_myers() {
check_files("tests/Hello.java",
"tests/Test0.txt",
Box::new(MyersConfig::new()))
}

#[test]
fn test_comment_hello_myers() {
check_files("tests/Comment.java",
"tests/Hello.java",
Box::new(MyersConfig::new()))
}

#[test]
fn test_short1_wiki2_myers() {
check_files("tests/short1.txt",
"tests/wiki2.txt",
Box::new(MyersConfig::new()))
}

0 comments on commit ce44009

Please sign in to comment.