Skip to content

Commit

Permalink
Include source path in dump JSON (#2466)
Browse files Browse the repository at this point in the history
  • Loading branch information
psibi authored Dec 11, 2024
1 parent 5c691a8 commit 5393105
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
unstable_features.insert(UnstableFeature::ScriptInterpreterSetting);
}

let source = root.to_owned();
let root = paths.get(root).unwrap();

Ok(Justfile {
Expand All @@ -207,7 +208,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
name,
recipes,
settings,
source: root.into(),
source,
unexports: self.unexports,
unstable_features,
warnings: self.warnings,
Expand Down
55 changes: 14 additions & 41 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,50 +447,23 @@ fn lowercase(_context: Context, s: &str) -> FunctionResult {
}

fn module_directory(context: Context) -> FunctionResult {
context
.evaluator
.context
.search
.justfile
.parent()
.unwrap()
.join(&context.evaluator.context.module.source)
.parent()
.unwrap()
.to_str()
.map(str::to_owned)
.ok_or_else(|| {
format!(
"Module directory is not valid unicode: {}",
context
.evaluator
.context
.module
.source
.parent()
.unwrap()
.display(),
)
})
let module_directory = context.evaluator.context.module.source.parent().unwrap();
module_directory.to_str().map(str::to_owned).ok_or_else(|| {
format!(
"Module directory is not valid unicode: {}",
module_directory.display(),
)
})
}

fn module_file(context: Context) -> FunctionResult {
context
.evaluator
.context
.search
.justfile
.parent()
.unwrap()
.join(&context.evaluator.context.module.source)
.to_str()
.map(str::to_owned)
.ok_or_else(|| {
format!(
"Module file path is not valid unicode: {}",
context.evaluator.context.module.source.display(),
)
})
let module_file = &context.evaluator.context.module.source;
module_file.to_str().map(str::to_owned).ok_or_else(|| {
format!(
"Module file path is not valid unicode: {}",
module_file.display(),
)
})
}

fn num_cpus(_context: Context) -> FunctionResult {
Expand Down
1 change: 0 additions & 1 deletion src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
31 changes: 26 additions & 5 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Interpreter<'a> {
command: &'a str,
}

#[derive(Debug, Default, PartialEq, Serialize)]
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Module<'a> {
aliases: BTreeMap<&'a str, Alias<'a>>,
Expand All @@ -44,6 +44,7 @@ struct Module<'a> {
settings: Settings<'a>,
unexports: Vec<&'a str>,
warnings: Vec<&'a str>,
source: PathBuf,
}

#[derive(Debug, Default, Deserialize, PartialEq, Serialize)]
Expand Down Expand Up @@ -98,8 +99,26 @@ fn case(justfile: &str, expected: Module) {
case_with_submodule(justfile, None, expected);
}

fn fix_source(dir: &Path, module: &mut Module) {
let filename = if module.source.as_os_str().is_empty() {
Path::new("justfile")
} else {
&module.source
};

module.source = if cfg!(target_os = "macos") {
dir.canonicalize().unwrap().join(filename)
} else {
dir.join(filename)
};

for module in module.modules.values_mut() {
fix_source(dir, module);
}
}

#[track_caller]
fn case_with_submodule(justfile: &str, submodule: Option<(&str, &str)>, expected: Module) {
fn case_with_submodule(justfile: &str, submodule: Option<(&str, &str)>, mut expected: Module) {
let mut test = Test::new()
.justfile(justfile)
.args(["--dump", "--dump-format", "json"])
Expand All @@ -109,11 +128,11 @@ fn case_with_submodule(justfile: &str, submodule: Option<(&str, &str)>, expected
test = test.write(path, source);
}

let actual = test.run().stdout;
fix_source(test.tempdir.path(), &mut expected);

let mut expected = serde_json::to_string(&expected).unwrap();
expected.push('\n');
let actual = test.run().stdout;

let actual: Module = serde_json::from_str(actual.as_str()).unwrap();
pretty_assertions::assert_eq!(actual, expected);
}

Expand Down Expand Up @@ -776,6 +795,7 @@ fn module() {
Module {
doc: Some("hello"),
first: Some("bar"),
source: "foo.just".into(),
recipes: [(
"bar",
Recipe {
Expand Down Expand Up @@ -808,6 +828,7 @@ fn module_group() {
Module {
first: Some("bar"),
groups: ["alpha"].into(),
source: "foo.just".into(),
recipes: [(
"bar",
Recipe {
Expand Down

0 comments on commit 5393105

Please sign in to comment.