Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/failed-download-d…
Browse files Browse the repository at this point in the history
…etection
  • Loading branch information
dennismeister93 committed Jun 7, 2024
2 parents 0cee628 + 6d39c84 commit 7b16f59
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/commands/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ export default class Package extends Command {
if (args.name) {
packagesToPrint.push(projectConfig.getPackages().find((pkgCfg: PackageConfig) => pkgCfg.getPackageName() === args.name)!);

const componentDir = join(packagesToPrint[0].getPackageDirectory(), packagesToPrint[0].version);

if (flags.getPath) {
this.log(componentDir);
this.log(packagesToPrint[0].getPackageDirectoryWithVersion());
return;
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/commands/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Syncing Velocitas components!
this.log(`Syncing Velocitas components!`);
const projectConfig = ProjectConfigIO.read(`v${this.config.version}`);

for (const component of projectConfig.getComponentContexts()) {
if (!component.manifest.files || component.manifest.files.length === 0) {
for (const componentContext of projectConfig.getComponentContexts()) {
if (!componentContext.manifest.files || componentContext.manifest.files.length === 0) {
continue;
}

this.log(`... syncing '${component.manifest.id}'`);
installComponent(component.packageConfig, component.manifest, projectConfig.getVariableCollection(component));
this.log(`... syncing '${componentContext.manifest.id}'`);
installComponent(componentContext, projectConfig.getVariableCollection(componentContext));
}
}
}
12 changes: 12 additions & 0 deletions src/modules/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//
// SPDX-License-Identifier: Apache-2.0

import { join } from 'node:path';
import { PackageConfig } from './package';
import { VariableDefinition } from './variables';

Expand Down Expand Up @@ -75,6 +76,9 @@ export interface ComponentManifest {
// Human readable description of the component, if any.
description?: string;

// A base path within the package where all files and programs are located to relatively.
basePath?: string;

// A list of files that need to be copied from source to target when running `velocitas sync`.
files?: FileSpec[];

Expand Down Expand Up @@ -114,4 +118,12 @@ export class ComponentContext {
this.config = config;
this.usedInProject = usedInProject;
}

getComponentPath(): string {
const componentPath = this.packageConfig.getPackageDirectoryWithVersion();
if (this.manifest.basePath) {
return join(componentPath, this.manifest.basePath);
}
return componentPath;
}
}
2 changes: 1 addition & 1 deletion src/modules/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function runExecSpec(
throw new Error(`No program found for item '${execSpec.ref}' referenced in program list of '${componentId}'`);
}

const cwd = join(componentContext.packageConfig.getPackageDirectory(), componentContext.packageConfig.version);
const cwd = componentContext.getComponentPath();

let programArgs = programSpec.args ? programSpec.args : [];
if (execSpec.args && execSpec.args.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function getVelocitasRoot(): string {
return join(process.env.VELOCITAS_HOME ? process.env.VELOCITAS_HOME : homedir(), '.velocitas');
}

function getPackageFolderPath(): string {
export function getPackageFolderPath(): string {
return join(getVelocitasRoot(), 'packages');
}

Expand Down
10 changes: 5 additions & 5 deletions src/modules/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { cwd } from 'node:process';
import { Transform, TransformCallback, TransformOptions } from 'node:stream';
import copy from 'recursive-copy';
import { CliFileSystem } from '../utils/fs-bridge';
import { ComponentManifest } from './component';
import { ComponentContext, ComponentManifest } from './component';
import { PackageConfig } from './package';
import { VariableCollection } from './variables';

Expand Down Expand Up @@ -80,15 +80,15 @@ class ReplaceVariablesStream extends Transform {
}
}

export function installComponent(packageConfig: PackageConfig, component: ComponentManifest, variables: VariableCollection) {
if (component.files) {
for (const spec of component.files) {
export function installComponent(component: ComponentContext, variables: VariableCollection) {
if (component.manifest.files) {
for (const spec of component.manifest.files) {
const src = variables.substitute(spec.src);
const dst = variables.substitute(spec.dst);
let ifCondition = spec.condition ? variables.substitute(spec.condition) : 'true';

if (eval(ifCondition)) {
const sourceFileOrDir = join(packageConfig.getPackageDirectory(), packageConfig.version, src);
const sourceFileOrDir = join(component.getComponentPath(), src);
const destFileOrDir = join(cwd(), dst);
try {
if (CliFileSystem.existsSync(sourceFileOrDir)) {
Expand Down
7 changes: 7 additions & 0 deletions test/system-test/exec.stest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ describe('CLI command', () => {
});
expect(result.status).to.be.equal(1);
});

it('should execute programs with relative paths', async () => {
const result = spawnSync(VELOCITAS_PROCESS, ['exec', 'test-component2', 'nested'], {
encoding: DEFAULT_BUFFER_ENCODING,
});
expect(result.stdout.trim()).to.be.equal("hello nested");
});
});
});

Expand Down
7 changes: 7 additions & 0 deletions test/system-test/sync.stest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const packageManifestTwo = JSON.parse(

const fileOneDestination = packageManifestOne.components[0].files[0].dst;
const fileTwoDestination = packageManifestTwo.components[0].files[0].dst;
const fileThreeDestination = packageManifestTwo.components[1].files[0].dst;

describe('CLI command', () => {
describe('sync', () => {
Expand All @@ -38,6 +39,7 @@ describe('CLI command', () => {
afterEach(() => {
removeSync(`./${fileOneDestination}`);
removeSync(`./${fileTwoDestination}`);
removeSync(`./${fileThreeDestination}`);
});
it('should sync configured setup components and replace variables accordingly', async () => {
const syncOutput = spawnSync(VELOCITAS_PROCESS, ['sync'], { encoding: DEFAULT_BUFFER_ENCODING });
Expand All @@ -56,6 +58,11 @@ describe('CLI command', () => {
expect(resultTwo.stdout).to.contain('projectTest');
expect(resultTwo.stdout).to.contain('packageTestTwo');
expect(resultTwo.stdout).to.contain(2);

const resultThree = readFileSync(`./${fileThreeDestination}`, {
encoding: DEFAULT_BUFFER_ENCODING,
});
expect(resultThree).to.equal('A nested file\n');
});
});
});
44 changes: 44 additions & 0 deletions test/unit/component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// This program and the accompanying materials are made available under the
// terms of the Apache License, Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// SPDX-License-Identifier: Apache-2.0

import { expect } from 'chai';
import 'mocha';
import { ComponentConfig, ComponentContext, ComponentManifest } from '../../src/modules/component';
import { PackageConfig, getPackageFolderPath } from '../../src/modules/package';
import { join } from 'path';

function createDefaultComponentContext(basePath?: string): ComponentContext {
const componentId = "componentUnderTest";
const pkgConfig = new PackageConfig({ repo: "packageUnderTest", version: "main" });
const componentManifest: ComponentManifest = {
id: componentId,
basePath: basePath
};
const componentConfig = new ComponentConfig(componentId);
return new ComponentContext(pkgConfig, componentManifest, componentConfig, true);
}

describe('component - module', () => {
describe('ComponentContext`s getComponentPath function', () => {
it('should return the package path as the component path, if no base path is given', () => {
const componentContext = createDefaultComponentContext();
expect(componentContext.getComponentPath()).to.equal(join(getPackageFolderPath(), "packageUnderTest", "main"));
});

it('should return the package path extended by the basePath as the component path, if a base path is given', () => {
const componentContext = createDefaultComponentContext("foo/bar");
expect(componentContext.getComponentPath()).to.equal(join(getPackageFolderPath(), "packageUnderTest", "main", "foo", "bar"));
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0

print("hello nested")
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"components": [
{
"id": "test-component",
"type": "setup",
"programs": [
{
"id": "echo-env",
Expand All @@ -11,37 +10,68 @@
{
"id": "executable-on-path",
"executable": "python3",
"args": ["./hello-world.py"]
"args": [
"./hello-world.py"
]
},
{
"id": "set-cache",
"executable": "python3",
"args": ["./set-cache.py"]
"args": [
"./set-cache.py"
]
},
{
"id": "get-cache",
"executable": "python3",
"args": ["./get-cache.py"]
"args": [
"./get-cache.py"
]
},
{
"id": "tty",
"executable": "docker",
"args": ["run", "-it", "hello-world"]
"args": [
"run",
"-it",
"hello-world"
]
},
{
"id": "print-args",
"executable": "python3",
"args": ["./print-args.py", "default", "foo"]
"args": [
"./print-args.py",
"default",
"foo"
]
},
{
"id": "print-args-no-default",
"executable": "python3",
"args": ["./print-args.py"]
"args": [
"./print-args.py"
]
},
{
"id": "exit",
"executable": "python3",
"args": ["./exit.py"]
"args": [
"./exit.py"
]
}
]
},
{
"id": "test-component2",
"basePath": "components/test-component2",
"programs": [
{
"id": "nested",
"executable": "python3",
"args": [
"./nested.py"
]
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion testbench/test-sync/.velocitas.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
},
"components": [
"test-componentOne",
"test-componentTwo"
"test-componentTwo",
"test-componentThree"
],
"variables": {
"projectVariable": "projectTest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"components": [
{
"id": "test-componentOne",
"type": "setup",
"files": [
{
"src": "testFile.sh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
"components": [
{
"id": "test-componentTwo",
"type": "setup",
"files": [
{
"src": "testFile.sh",
"dst": "testFile2.sh"
}
]
},
{
"id": "test-componentThree",
"basePath": "nested",
"files": [
{
"src": "dummy",
"dst": "dummy"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A nested file

0 comments on commit 7b16f59

Please sign in to comment.