Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add konfig integration tests on the format tool, fix test suites and add more unit test suites. #417

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kclvm/ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ impl<'p> Printer<'p> {
}
}

/// Wether has comments on ast node.
pub(crate) fn has_comments_on_node<T>(&mut self, node: &ast::NodeRef<T>) -> bool {
if !self.cfg.write_comments {
return false;
}
let mut index = None;
for (i, comment) in self.comments.iter().enumerate() {
if comment.line <= node.line {
index = Some(i);
} else {
break;
}
}
index.is_some()
}

/// Print ast comments.
pub fn write_ast_comments<T>(&mut self, node: &ast::NodeRef<T>) {
if !self.cfg.write_comments {
Expand Down
33 changes: 27 additions & 6 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,14 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
.iter()
.map(|e| e.line)
.collect::<HashSet<u64>>();
let mut in_one_line = line_set.len() <= 1;
// There are comments in the configuration block.
let has_comment = list_expr
.elts
.iter()
.map(|e| self.has_comments_on_node(e))
.all(|r| r);
// When there are comments in the configuration block, print them as multiline configurations.
let mut in_one_line = line_set.len() <= 1 && !has_comment;
if let Some(elt) = list_expr.elts.first() {
if let ast::Expr::ListIfItem(_) = &elt.node {
in_one_line = false;
Expand Down Expand Up @@ -527,6 +534,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}
self.write(dict_comp.entry.operation.symbol());
self.write_space();
self.expr(&dict_comp.entry.value);
for gen in &dict_comp.generators {
self.walk_comp_clause(&gen.node);
}
Expand Down Expand Up @@ -596,10 +604,23 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {

fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result {
let line_set: HashSet<u64> = config_expr.items.iter().map(|item| item.line).collect();
let mut in_one_line = line_set.len() <= 1;
if let Some(item) = config_expr.items.first() {
if let ast::Expr::ConfigIfEntry(_) = &item.node.value.node {
in_one_line = false;
// There are comments in the configuration block.
let has_comment = config_expr
.items
.iter()
.map(|item| self.has_comments_on_node(item))
.all(|r| r);
// When there are comments in the configuration block, print them as multiline configurations.
let mut in_one_line = line_set.len() <= 1 && !has_comment;
// When there are complex configuration blocks in the configuration block, print them as multiline configurations.
if config_expr.items.len() == 1 && in_one_line {
if let Some(item) = config_expr.items.first() {
if matches!(
&item.node.value.node,
ast::Expr::ConfigIfEntry(_) | ast::Expr::Config(_) | ast::Expr::Schema(_)
) {
in_one_line = false;
}
}
}
self.write_token(TokenKind::OpenDelim(DelimToken::Brace));
Expand Down Expand Up @@ -649,7 +670,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}
self.write_space();
self.write_token(TokenKind::OpenDelim(DelimToken::Brace));
self.write_newline();
self.write_newline_without_fill();
self.write_indentation(Indentation::Indent);

// lambda body
Expand Down
4 changes: 3 additions & 1 deletion kclvm/ast_pretty/src/test_data/comment.output
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ appConfiguration = AppConfiguration {
disk: "50Gi"
memory: "12Gi"
}
labels: {key: {key: 12}}
labels: {
key: {key: 12}
}
# Comment Six
mainContainer: Main {name: "kusion_override"}
# Comment Seven
Expand Down
8 changes: 6 additions & 2 deletions kclvm/query/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ schema AppConfiguration:
appConfiguration = AppConfiguration {
appName: "kclvm"
image: "kcl/kcl:{}".format(version)
labels: {key: {key: "override_value"}}
labels: {
key: {key: "override_value"}
}
mainContainer: Main {name: "override_name"}
overQuota = False
overQuota = False
Expand All @@ -118,7 +120,9 @@ appConfigurationUnification: AppConfiguration {
disk: "50Gi"
memory: "12Gi"
}
labels: {key: {key: "override_value"}}
labels: {
key: {key: "override_value"}
}
mainContainer: Main {name: "override_name"}
overQuota: False
}
Expand Down
1 change: 0 additions & 1 deletion kclvm/runner/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use kclvm_utils::path::PathPrefix;
use std::env::consts::DLL_SUFFIX;
use std::ffi::CString;
use std::path::Path;
use std::path::PathBuf;

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ aa = 1
assert aa == 1 if aa, "message"
aaaa = (1 + 2 / 2) if _a == 2 + +134.3 else ("a" * 3)
bbbb = "{}".format(a)
config: Config {
main: Container {
name: "main"
image: "test/test:v2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ log = math. log(12)
aa = 1
assert aa == 1 if aa,"message"
aaaa = (1 + 2 / 2) if _a == 2 + + 134.3 else ("a"*3)
bbbb = "{}". format(a)
bbbb = "{}". format(a)
config: Config {
main: Container {
name: "main"
image: "test/test:v2"
}

}
4 changes: 4 additions & 0 deletions kclvm/tools/src/format/test_data/format_data/comment.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ schema Person:
# Block comment in schema
age: int = 18

config = {
# Data comment
data = 1
}
7 changes: 6 additions & 1 deletion kclvm/tools/src/format/test_data/format_data/comment.input
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ schema Person:
"""
name:str="Alice"# Inline comment in schema
# Block comment in schema
age:int=18
age:int=18

config = {
# Data comment
data = 1
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
data0 = [i + 1 for i in range(10) if i > 1]
data1 = [i + 1 for i in range(10) if i > 1]
data2 = {k = v for k in ["k1", "k2"] for v in [1, 2]}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ data1 = [
i + 1 for i in range(10)
if i > 1
]
data2 = {k = v for k in ["k1", "k2"] for v in [1, 2]}
10 changes: 10 additions & 0 deletions kclvm/tools/src/format/test_data/format_data/lambda.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ f1 = lambda x: int, y: int -> int {
x + y

}
f2 = lambda {
lambda x {
lambda y {
y + 1

}

}

}
7 changes: 7 additions & 0 deletions kclvm/tools/src/format/test_data/format_data/lambda.input
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ f0 = lambda {
f1 = lambda x : int , y : int ->int{
x + y
}
f2 = lambda {
lambda x {
lambda y {
y + 1
}
}
}
60 changes: 60 additions & 0 deletions kclvm/tools/src/format/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;
use kclvm_parser::parse_file;
use pretty_assertions::assert_eq;
use walkdir::WalkDir;

const FILE_INPUT_SUFFIX: &str = ".input";
const FILE_OUTPUT_SUFFIX: &str = ".golden";
Expand Down Expand Up @@ -86,3 +88,61 @@ fn test_format_with_stdout_option() {
let changed_files = format("./src/format/test_data/format_path_data/", &opts).unwrap();
assert_eq!(changed_files.len(), 2);
}

#[test]
fn test_format_integration_konfig() -> Result<()> {
let konfig_path = Path::new(".")
.canonicalize()?
.parent()
.unwrap()
.parent()
.unwrap()
.join("test")
.join("konfig");
let files = get_files(konfig_path, true, true, ".k");
for file in &files {
// Skip test and hidden files.
if file.ends_with("_test.k") || file.starts_with("_") {
continue;
}
assert!(
parse_file(file, None).is_ok(),
"file {} test format failed",
file
);
let src = std::fs::read_to_string(file)?;
let (formatted_src, _) = format_source(&src)?;
let parse_result = parse_file("test.k", Some(formatted_src.clone() + "\n"));
assert!(
parse_result.is_ok(),
"file {} test format failed, the formatted source is\n{}\n the parse error is\n{}",
file,
formatted_src,
parse_result.err().unwrap(),
);
}
Ok(())
}

/// Get kcl files from path.
fn get_files<P: AsRef<Path>>(
path: P,
recursively: bool,
sorted: bool,
suffix: &str,
) -> Vec<String> {
let mut files = vec![];
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() {
let file = path.to_str().unwrap();
if file.ends_with(suffix) && (recursively || entry.depth() == 1) {
files.push(file.to_string())
}
}
}
if sorted {
files.sort();
}
files
}
1 change: 1 addition & 0 deletions kclvm/tools/src/vet/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
use anyhow::{Context, Result};

use crate::util::loader::LoaderKind;
#[cfg(target_os = "windows")]
use kclvm_runtime::PanicInfo;

const CARGO_DIR: &str = env!("CARGO_MANIFEST_DIR");
Expand Down