Skip to content

Commit

Permalink
add rust-plain, python-pyproject tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deciduously committed Jan 14, 2025
1 parent 4f84910 commit 90dd67d
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 8 deletions.
77 changes: 77 additions & 0 deletions packages/autobuild/python/tangram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as std from "std" with { path: "../../std" };
import * as python from "python" with { path: "../../python" };
// import * as poetry from "poetry" with { path: "../../poetry" };
import { wrapScripts } from "../common";

export type Arg = {
build?: string;
env?: std.env.Arg;
host?: string;
source: tg.Directory;
};

export const build = tg.target(async (arg: Arg) => {
const { build, env: envArg, host, source } = arg ?? {};

const env_ = envArg ?? std.env.arg(env({ build, host }), envArg);
let arg_: python.BuildArg = { build, env: env_, host, source };

const maybeRequirements = source.tryGet("requirements.txt");
if (maybeRequirements) {
if (maybeRequirements instanceof tg.File) {
arg_ = { ...arg_, requirements: maybeRequirements };
}
}

return python.build(arg_);
});

export default build;

export const plain = tg.target(async (arg: Arg) => {
const { build, env: envArg, host, source } = arg ?? {};

const env_ = envArg ?? std.env.arg(env({ build, host }), envArg);
const toolchain = await python.toolchain();
const interpreter = await toolchain.get("bin/python3").then(tg.File.expect);
return wrapScripts({
directory: source,
extension: ".py",
interpreter,
env: std.env.arg(
{
PYTHONPATH: toolchain,
},
env_,
),
});
});

// export const poetry = tg.target(async (arg: Arg) => {
// const { build, env: envArg, host, source } = arg ?? {};

// const env_ = envArg ?? std.env.arg(env({ build, host }), envArg);
// const arg_ = { build, env: env_, host, source };
// return poetry.build(arg_);
// });

export const pyproject = tg.target(async (arg: Arg) => {
const { build, env: envArg, host, source } = arg ?? {};

const env_ = envArg ?? std.env.arg(env({ build, host }), envArg);
const pyprojectToml = await source.get("pyproject.toml").then(tg.File.expect);
const arg_ = { build, env: env_, host, pyprojectToml, source };
return python.build(arg_);
});

type EnvArg = {
build?: string | undefined;
host?: string | undefined;
};

export const env = tg.target(async (arg: EnvArg) => {
const { build: build_, host: host_ } = arg ?? {};
const host = host_ ?? (await std.triple.host());
const build = build_ ?? host;
return std.env(python.toolchain({ ...std.triple.rotate({ build, host }) }));
});
55 changes: 52 additions & 3 deletions packages/autobuild/tangram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ import * as autotools from "./autotools";
import * as cmake from "./cmake";
import * as go from "./go";
import * as js from "./js";
import * as python from "./python";
import * as rust from "./rust";

import ccAutotoolsTest from "./tests/cc-autotools" with { type: "directory" };
import cmakeTest from "./tests/cmake" with { type: "directory" };
import goTest from "./tests/go" with { type: "directory" };
import jsNodeTest from "./tests/js-node" with { type: "directory" };
import jsPlainTest from "./tests/js-plain" with { type: "directory" };
import pythonTest from "./tests/python" with { type: "directory" };
import pythonPlainTest from "./tests/python-plain" with { type: "directory" };
import pythonPyprojectTest from "./tests/python-pyproject" with {
type: "directory",
};
import rustCargoTest from "./tests/rust-cargo" with { type: "directory" };
import rustPlainTest from "./tests/rust-plain" with { type: "directory" };

export const metadata = {
name: "autobuild",
Expand Down Expand Up @@ -57,9 +64,21 @@ export const build = tg.target(async (arg: Arg) => {
case "js-plain": {
return js.plain(arg_);
}
case "python": {
return python.build(arg_);
}
case "python-plain": {
return python.plain(arg_);
}
case "python-pyproject": {
return python.pyproject(arg_);
}
case "rust-cargo": {
return rust.cargo(arg_);
}
case "rust-plain": {
return rust.plain(arg_);
}
default: {
throw new Error(
`unable to autodetect project type, edit your tangram.ts file to define desired behavior`,
Expand Down Expand Up @@ -98,7 +117,13 @@ export const env = tg.target(async (arg: EnvArg) => {
case "js-plain": {
return js.env(arg_);
}
case "rust-cargo": {
case "python":
case "python-plain":
case "python-pyproject": {
return python.env(arg_);
}
case "rust-cargo":
case "rust-plain": {
return rust.env(arg_);
}
default: {
Expand All @@ -115,7 +140,11 @@ export type Kind =
| "go"
| "js-node"
| "js-plain"
| "rust-cargo";
| "python"
| "python-plain"
| "python-pyproject"
| "rust-cargo"
| "rust-plain";

export const detectKind = async (source: tg.Directory): Promise<Kind> => {
const entries = await source.entries();
Expand All @@ -137,8 +166,11 @@ export const detectKind = async (source: tg.Directory): Promise<Kind> => {
if (hasExecutableFile("configure") || hasFile("configure.ac"))
return "cc-autotools";
if (hasFile("package.json")) return "js-node";
if (hasFile("pyproject.toml")) return "python-pyproject";
if (hasFile("setup.py") || hasFile("setup.cfg")) return "python";
if (hasFile("go.mod") || hasDir("vendor")) return "go";

if (hasFileWithExtension(".py")) return "python-plain";
if (hasFileWithExtension(".rs")) return "rust-plain";
if (hasFileWithExtension(".js")) return "js-plain";

Expand All @@ -151,9 +183,10 @@ export const test = tg.target(async () => {
"cc-autotools",
"cmake",
"go",
// "js-node",// failed to creat target without host - phases.build issue?
"js-plain",
"python-plain",
"rust-cargo",
"rust-plain",
];
await Promise.all(allKinds.map((variant) => testKind(variant)));

Expand Down Expand Up @@ -190,7 +223,19 @@ const testParamaters = (): Record<Kind, TestFnArg> => {
testFile: (buildOutput: tg.Directory): Promise<tg.Template> =>
tg`${buildOutput}/index.js`,
},
python: defaultTestArg,
"python-plain": {
...defaultTestArg,
testFile: (buildOutput: tg.Directory): Promise<tg.Template> =>
tg`${buildOutput}/main.py`,
},
"python-pyproject": defaultTestArg,
"rust-cargo": defaultTestArg,
"rust-plain": {
...defaultTestArg,
testFile: (buildOutput: tg.Directory): Promise<tg.Template> =>
tg`${buildOutput}/bin/main`,
},
};
};

Expand All @@ -202,7 +247,11 @@ const testDirs = async (): Promise<Record<Kind, tg.Directory>> => {
go: goTest,
"js-node": jsNodeTest,
"js-plain": jsPlainTest,
python: pythonTest,
"python-plain": pythonPlainTest,
"python-pyproject": pythonPyprojectTest,
"rust-cargo": rustCargoTest,
"rust-plain": rustPlainTest,
};
};

Expand Down
1 change: 1 addition & 0 deletions packages/autobuild/tests/python-plain/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello, world!")
3 changes: 3 additions & 0 deletions packages/autobuild/tests/python-pyproject/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# demo

A sample Poetry package.
20 changes: 20 additions & 0 deletions packages/autobuild/tests/python-pyproject/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "demo_package"
version = "0.1.0"
description = "A demonstration package that displays text to stdout"
readme = "README.md"
authors = [
{name = "Tangram", email = "[email protected]"}
]
requires-python = ">=3.8"
dependencies = []

[project.optional-dependencies]
dev = ["pytest>=7.0"]

[project.scripts]
test = "demo_package.main:main"
Empty file.
10 changes: 10 additions & 0 deletions packages/autobuild/tests/python-pyproject/src/demo_package/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def get_message() -> str:
return "Hello, world!"

def main() -> None:
"""Entry point for the application."""
message = get_message()
print(message)

if __name__ == "__main__":
main()
Empty file.
3 changes: 3 additions & 0 deletions packages/autobuild/tests/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# demo

A sample Poetry package.
21 changes: 21 additions & 0 deletions packages/autobuild/tests/python/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[metadata]
name = demo-package
version = 0.1.0
description = A demonstration package that displays text to stdout
long_description = file: README.md
long_description_content_type = text/markdown
author = Tangram
author_email = [email protected]

[options]
package_dir =
= src
packages = find:
python_requires = >=3.8

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
demo-text = demo_package.main:main
Empty file.
10 changes: 10 additions & 0 deletions packages/autobuild/tests/python/src/demo_package/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def get_message() -> str:
return "Hello world!"

def main() -> None:
"""Entry point for the application."""
message = get_message()
print(message)

if __name__ == "__main__":
main()
Empty file.
3 changes: 3 additions & 0 deletions packages/autobuild/tests/rust-plain/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
2 changes: 1 addition & 1 deletion packages/python/tangram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export const build = tg.target(async (...args: std.Args<BuildArg>) => {
toolchain({ ...pythonArg, build: buildTriple, host }),
{
["lib/python3/site-packages"]: {
[name]: tg.symlink(tg`${source}/${name}`),
[name]: tg.symlink(tg`${source}/src/${name}`),
},
},
);
Expand Down
10 changes: 6 additions & 4 deletions packages/ruby/tangram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ export const metadata = {
name: "ruby",
license: "BSD-2-Clause",
repository: "https://git.ruby-lang.org/ruby.git",
version: "3.3.0",
version: "3.4.1",
};

export const source = tg.target(async () => {
const { version } = metadata;
const checksum =
"sha256:96518814d9832bece92a85415a819d4893b307db5921ae1f0f751a9a89a56b7d";
"sha256:3d385e5d22d368b064c817a13ed8e3cc3f71a7705d7ed1bae78013c33aa7c87f";
const extension = ".tar.gz";
const majorMinor = version.split(".").slice(0, 2).join(".");
const url = `https://cache.ruby-lang.org/pub/ruby/${majorMinor}/ruby-${version}${extension}`;
const outer = tg.Directory.expect(await std.download({ url, checksum }));
return std.directory.unwrap(outer);
return std
.download({ url, checksum })
.then(tg.Directory.expect)
.then(std.directory.unwrap);
});

export type Arg = {
Expand Down

0 comments on commit 90dd67d

Please sign in to comment.