1
1
mirror of https://github.com/casey/just.git synced 2024-11-22 02:09:44 +03:00
This commit is contained in:
Sibi Prabakaran 2024-11-19 07:43:07 +05:30 committed by GitHub
commit b44cd99ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1059 additions and 937 deletions

View File

@ -182,6 +182,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
unstable_features.insert(UnstableFeature::ScriptInterpreterSetting);
}
let source = root.canonicalize().unwrap();
let root = paths.get(root).unwrap();
Ok(Justfile {
@ -205,7 +206,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
name,
recipes,
settings,
source: root.into(),
source,
unexports: self.unexports,
unstable_features,
warnings: self.warnings,

View File

@ -23,7 +23,6 @@ pub(crate) struct Justfile<'src> {
pub(crate) name: Option<Name<'src>>,
pub(crate) recipes: Table<'src, Rc<Recipe<'src>>>,
pub(crate) settings: Settings<'src>,
#[serde(skip)]
pub(crate) source: PathBuf,
pub(crate) unexports: HashSet<String>,
#[serde(skip)]

View File

@ -1,22 +1,37 @@
use super::*;
fn case(justfile: &str, value: Value) {
fn case<F: Fn(&Path) -> Value>(justfile: &str, value: F) {
Test::new()
.justfile(justfile)
.args(["--dump", "--dump-format", "json"])
.stdout(format!("{}\n", serde_json::to_string(&value).unwrap()))
.stdout_with_tempdir(|dir| {
format!(
"{}\n",
serde_json::to_string(&value(&dir.path().canonicalize().unwrap())).unwrap()
)
})
.run();
}
#[test]
fn alias() {
case(
let test = Test::new().justfile(
"
alias f := foo
foo:
",
json!({
);
let source = test
.tempdir
.path()
.canonicalize()
.unwrap()
.join("justfile")
.to_str()
.unwrap()
.to_owned();
let json = json!({
"first": "foo",
"doc": null,
"aliases": {
@ -66,14 +81,18 @@ fn alias() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
});
test
.args(["--dump", "--dump-format", "json"])
.stdout(format!("{}\n", serde_json::to_string(&json).unwrap()))
.run();
}
#[test]
fn assignment() {
case(
"foo := 'bar'",
case("foo := 'bar'", |dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {
@ -110,8 +129,9 @@ fn assignment() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
})
});
}
#[test]
@ -122,6 +142,8 @@ fn private_assignment() {
[private]
bar := 'bar'
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {
@ -164,7 +186,9 @@ fn private_assignment() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -176,6 +200,8 @@ fn body() {
bar
abc{{ 'xyz' }}def
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -222,7 +248,9 @@ fn body() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -233,6 +261,8 @@ fn dependencies() {
foo:
bar: foo
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -292,7 +322,9 @@ fn dependencies() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -316,6 +348,8 @@ fn dependency_argument() {
replace('a', 'b', 'c')
)
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"first": "foo",
@ -400,7 +434,9 @@ fn dependency_argument() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -414,6 +450,8 @@ fn duplicate_recipes() {
foo:
foo bar:
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"first": "foo",
"doc": null,
@ -470,7 +508,9 @@ fn duplicate_recipes() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -482,6 +522,8 @@ fn duplicate_variables() {
x := 'foo'
x := 'bar'
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {
@ -518,14 +560,16 @@ fn duplicate_variables() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
#[test]
fn doc_comment() {
case(
"# hello\nfoo:",
case("# hello\nfoo:", |dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"first": "foo",
@ -569,14 +613,15 @@ fn doc_comment() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
})
});
}
#[test]
fn empty_justfile() {
case(
"",
case("", |dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -606,8 +651,9 @@ fn empty_justfile() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
})
});
}
#[test]
@ -621,6 +667,8 @@ fn parameters() {
e *x:
f $x:
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"first": "a",
@ -764,7 +812,9 @@ fn parameters() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -776,6 +826,8 @@ fn priors() {
b: a && c
c:
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -855,14 +907,16 @@ fn priors() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
#[test]
fn private() {
case(
"_foo:",
case("_foo:", |dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -906,14 +960,15 @@ fn private() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
})
});
}
#[test]
fn quiet() {
case(
"@foo:",
case("@foo:", |dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -957,8 +1012,9 @@ fn quiet() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
})
});
}
#[test]
@ -977,6 +1033,8 @@ fn settings() {
foo:
#!bar
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -1023,7 +1081,9 @@ fn settings() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -1034,6 +1094,8 @@ fn shebang() {
foo:
#!bar
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -1077,14 +1139,16 @@ fn shebang() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
#[test]
fn simple() {
case(
"foo:",
case("foo:", |dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -1128,8 +1192,9 @@ fn simple() {
},
"unexports": [],
"warnings": [],
}),
);
"source": source,
})
});
}
#[test]
@ -1139,6 +1204,8 @@ fn attribute() {
[no-exit-message]
foo:
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -1182,7 +1249,9 @@ fn attribute() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -1199,7 +1268,24 @@ fn module() {
"foo.just": "bar:",
})
.args(["--dump", "--dump-format", "json"])
.stdout(format!(
.stdout_with_tempdir(|dir| {
let source = dir
.path()
.canonicalize()
.unwrap()
.join("justfile")
.to_str()
.unwrap()
.to_owned();
let foo_just = dir
.path()
.canonicalize()
.unwrap()
.join("foo.just")
.to_str()
.unwrap()
.to_owned();
format!(
"{}\n",
serde_json::to_string(&json!({
"aliases": {},
@ -1251,6 +1337,7 @@ fn module() {
},
"unexports": [],
"warnings": [],
"source": foo_just
},
},
"recipes": {},
@ -1275,9 +1362,11 @@ fn module() {
},
"unexports": [],
"warnings": [],
"source": source
}))
.unwrap()
))
)
})
.run();
}
@ -1294,7 +1383,24 @@ fn module_group() {
"foo.just": "bar:",
})
.args(["--dump", "--dump-format", "json"])
.stdout(format!(
.stdout_with_tempdir(|dir| {
let source = dir
.path()
.canonicalize()
.unwrap()
.join("justfile")
.to_str()
.unwrap()
.to_owned();
let foo_just = dir
.path()
.canonicalize()
.unwrap()
.join("foo.just")
.to_str()
.unwrap()
.to_owned();
format!(
"{}\n",
serde_json::to_string(&json!({
"aliases": {},
@ -1346,6 +1452,7 @@ fn module_group() {
},
"unexports": [],
"warnings": [],
"source": foo_just,
},
},
"recipes": {},
@ -1370,9 +1477,11 @@ fn module_group() {
},
"unexports": [],
"warnings": [],
"source": source,
}))
.unwrap()
))
)
})
.run();
}
@ -1383,6 +1492,8 @@ fn recipes_with_private_attribute_are_private() {
[private]
foo:
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -1426,7 +1537,9 @@ fn recipes_with_private_attribute_are_private() {
},
"unexports": [],
"warnings": [],
}),
"source": source,
})
},
);
}
@ -1438,6 +1551,8 @@ fn doc_attribute_overrides_comment() {
[doc('ATTRIBUTE')]
foo:
",
|dir| {
let source = dir.join("justfile").to_str().unwrap().to_owned();
json!({
"aliases": {},
"assignments": {},
@ -1481,6 +1596,8 @@ fn doc_attribute_overrides_comment() {
},
"unexports": [],
"warnings": [],
}),
"source": source
})
},
);
}

View File

@ -167,6 +167,11 @@ impl Test {
self
}
pub(crate) fn stdout_with_tempdir(mut self, stdout_fn: impl Fn(&TempDir) -> String) -> Self {
self.stdout = stdout_fn(&self.tempdir);
self
}
pub(crate) fn stdout_regex(mut self, stdout_regex: impl AsRef<str>) -> Self {
self.stdout_regex = Some(Regex::new(&format!("^{}$", stdout_regex.as_ref())).unwrap());
self