示例#1
0
def test_project_environment_ld_library_path_null(simple_project, new_dir):
    """LD_LIBRARY_PATH can be overridden without affecting PATH."""
    environment = {"LD_LIBRARY_PATH": None}
    snap_yaml.write(
        simple_project(environment=environment),
        prime_dir=Path(new_dir),
        arch="amd64",
        arch_triplet="x86_64-linux-gnu",
    )
    yaml_file = Path("meta/snap.yaml")
    assert yaml_file.is_file()

    content = yaml_file.read_text()
    assert content == textwrap.dedent("""\
        name: mytest
        version: 1.29.3
        summary: Single-line elevator pitch for your amazing snap
        description: test-description
        architectures:
        - amd64
        base: core22
        apps:
          app1:
            command: bin/mytest
        confinement: strict
        grade: stable
        environment:
          PATH: $SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH
        """)
示例#2
0
def test_simple_snap_yaml(simple_project, new_dir):
    snap_yaml.write(
        simple_project(),
        prime_dir=Path(new_dir),
        arch="amd64",
        arch_triplet="x86_64-linux-gnu",
    )
    yaml_file = Path("meta/snap.yaml")
    assert yaml_file.is_file()

    content = yaml_file.read_text()
    assert content == textwrap.dedent("""\
        name: mytest
        version: 1.29.3
        summary: Single-line elevator pitch for your amazing snap
        description: test-description
        architectures:
        - amd64
        base: core22
        apps:
          app1:
            command: bin/mytest
        confinement: strict
        grade: stable
        environment:
          LD_LIBRARY_PATH: ${SNAP_LIBRARY_PATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
          PATH: $SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH
        """)
示例#3
0
def test_project_environment_ld_library_path_and_path_defined(
        simple_project, new_dir):
    """Test behavior of defining LD_LIBRARY_PATH and PATH variables."""
    environment = {
        "LD_LIBRARY_PATH": "test-1",
        "PATH": "test-2",
    }
    snap_yaml.write(
        simple_project(environment=environment),
        prime_dir=Path(new_dir),
        arch="amd64",
        arch_triplet="x86_64-linux-gnu",
    )
    yaml_file = Path("meta/snap.yaml")
    assert yaml_file.is_file()

    content = yaml_file.read_text()
    assert content == textwrap.dedent("""\
        name: mytest
        version: 1.29.3
        summary: Single-line elevator pitch for your amazing snap
        description: test-description
        architectures:
        - amd64
        base: core22
        apps:
          app1:
            command: bin/mytest
        confinement: strict
        grade: stable
        environment:
          LD_LIBRARY_PATH: test-1
          PATH: test-2
        """)
示例#4
0
    def test_run_linters(self, mocker, new_dir, linter_issue):
        mocker.patch("snapcraft.linters.linters._LINTERS",
                     {"test": TestLinterRun._TestLinter})
        yaml_data = {
            "name": "mytest",
            "version": "1.29.3",
            "base": "core22",
            "summary": "Single-line elevator pitch for your amazing snap",
            "description": "test-description",
            "confinement": "strict",
            "parts": {},
        }

        project = projects.Project.unmarshal(yaml_data)
        snap_yaml.write(
            project,
            prime_dir=Path(new_dir),
            arch="amd64",
            arch_triplet="x86_64-linux-gnu",
        )

        issues = linters.run_linters(new_dir, lint=None)
        assert issues == [
            linters.LinterIssue(
                name="test",
                result=LinterResult.WARNING,
                text="Something wrong.",
                url="https://some/url",
            )
        ]
示例#5
0
def _generate_metadata(
    *,
    project: Project,
    lifecycle: PartsLifecycle,
    project_dir: Path,
    assets_dir: Path,
    start_time: datetime,
    parsed_args: "argparse.Namespace",
):
    project_vars = lifecycle.project_vars

    emit.progress("Extracting and updating metadata...")
    metadata_list = lifecycle.extract_metadata()
    update_project_metadata(
        project,
        project_vars=project_vars,
        metadata_list=metadata_list,
        assets_dir=assets_dir,
        prime_dir=lifecycle.prime_dir,
    )

    emit.progress("Copying snap assets...")
    setup_assets(
        project,
        assets_dir=assets_dir,
        project_dir=project_dir,
        prime_dir=lifecycle.prime_dir,
    )

    emit.progress("Generating snap metadata...")
    snap_yaml.write(
        project,
        lifecycle.prime_dir,
        arch=lifecycle.target_arch,
        arch_triplet=lifecycle.target_arch_triplet,
    )
    emit.progress("Generated snap metadata", permanent=True)

    if parsed_args.enable_manifest:
        _generate_manifest(
            project,
            lifecycle=lifecycle,
            start_time=start_time,
            parsed_args=parsed_args,
        )
示例#6
0
def test_version_git(simple_project, new_dir, mocker):
    """Version in projects with ``version:git`` must be correctly handled."""
    mocker.patch(
        "craft_parts.sources.git_source.GitSource.generate_version",
        return_value="1.2.3",
    )

    snap_yaml.write(
        simple_project(version="git"),
        prime_dir=Path(new_dir),
        arch="amd64",
        arch_triplet="x86_64-linux-gnu",
    )

    yaml_file = Path("meta/snap.yaml")
    content = yaml_file.read_text()

    data = yaml.safe_load(content)
    assert data["version"] == "1.2.3"
示例#7
0
def _run_command(
    command_name: str,
    *,
    project: Project,
    parse_info: Dict[str, List[str]],
    assets_dir: Path,
    start_time: datetime,
    parallel_build_count: int,
    parsed_args: "argparse.Namespace",
) -> None:
    managed_mode = utils.is_managed_mode()
    part_names = getattr(parsed_args, "parts", None)

    if not managed_mode:
        run_project_checks(project, assets_dir=assets_dir)

        if command_name == "snap":
            emit.progress(
                "The 'snap' command is deprecated, use 'pack' instead.",
                permanent=True,
            )

    if parsed_args.use_lxd and providers.get_platform_default_provider(
    ) == "lxd":
        emit.progress("LXD is used by default on this platform.",
                      permanent=True)

    if (not managed_mode and not parsed_args.destructive_mode
            and not os.getenv("SNAPCRAFT_BUILD_ENVIRONMENT") == "host"):
        if command_name == "clean" and not part_names:
            _clean_provider(project, parsed_args)
        else:
            _run_in_provider(project, command_name, parsed_args)
        return

    if managed_mode:
        work_dir = utils.get_managed_environment_home_path()
        project_dir = utils.get_managed_environment_project_path()
    else:
        work_dir = Path.cwd()
        project_dir = Path.cwd()

    step_name = "prime" if command_name in ("pack", "snap") else command_name

    lifecycle = PartsLifecycle(
        project.parts,
        work_dir=work_dir,
        assets_dir=assets_dir,
        base=project.get_effective_base(),
        package_repositories=project.package_repositories,
        parallel_build_count=parallel_build_count,
        part_names=part_names,
        adopt_info=project.adopt_info,
        project_name=project.name,
        parse_info=parse_info,
        project_vars={
            "version": project.version or "",
            "grade": project.grade or "",
        },
        extra_build_snaps=project.get_extra_build_snaps(),
    )

    if command_name == "clean":
        lifecycle.clean(part_names=part_names)
        return

    lifecycle.run(
        step_name,
        debug=parsed_args.debug,
        shell=getattr(parsed_args, "shell", False),
        shell_after=getattr(parsed_args, "shell_after", False),
    )

    # Extract metadata and generate snap.yaml
    project_vars = lifecycle.project_vars
    if step_name == "prime" and not part_names:
        emit.progress("Extracting and updating metadata...")
        metadata_list = lifecycle.extract_metadata()
        update_project_metadata(
            project,
            project_vars=project_vars,
            metadata_list=metadata_list,
            assets_dir=assets_dir,
            prime_dir=lifecycle.prime_dir,
        )

        emit.progress("Copying snap assets...")
        setup_assets(
            project,
            assets_dir=assets_dir,
            project_dir=project_dir,
            prime_dir=lifecycle.prime_dir,
        )

        emit.progress("Generating snap metadata...")
        snap_yaml.write(
            project,
            lifecycle.prime_dir,
            arch=lifecycle.target_arch,
            arch_triplet=lifecycle.target_arch_triplet,
        )
        emit.progress("Generated snap metadata", permanent=True)

        if parsed_args.enable_manifest:
            _generate_manifest(
                project,
                lifecycle=lifecycle,
                start_time=start_time,
                parsed_args=parsed_args,
            )

    if command_name in ("pack", "snap"):
        snap_filename = pack.pack_snap(
            lifecycle.prime_dir,
            output=parsed_args.output,
            compression=project.compression,
            name=project.name,
            version=process_version(project.version),
            target_arch=lifecycle.target_arch,
        )
        emit.message(f"Created snap package {snap_filename}")
示例#8
0
def test_complex_snap_yaml(complex_project, new_dir):
    snap_yaml.write(
        complex_project,
        prime_dir=Path(new_dir),
        arch="amd64",
        arch_triplet="x86_64-linux-gnu",
    )
    yaml_file = Path("meta/snap.yaml")
    assert yaml_file.is_file()

    content = yaml_file.read_text()
    assert content == textwrap.dedent("""\
        name: mytest
        version: 1.29.3
        summary: Single-line elevator pitch for your amazing snap
        description: |
          This is my-snap's description. You have a paragraph or two to tell the
          most important story about your snap. Keep it under 100 words though,
          we live in tweetspace and your description wants to look good in the snap
          store.
        type: app
        architectures:
        - amd64
        base: core22
        assumes:
        - command-chain
        apps:
          app1:
            command: bin/mytest
            autostart: test-app.desktop
            common-id: test-common-id
            bus-name: test-bus-name
            completer: test-completer
            stop-command: test-stop-command
            post-stop-command: test-post-stop-command
            start-timeout: 1s
            stop-timeout: 2s
            watchdog-timeout: 3s
            reload-command: test-reload-command
            restart-delay: 4s
            timer: test-timer
            daemon: simple
            after:
            - test-after-1
            - test-after-2
            before:
            - test-before-1
            - test-before-2
            refresh-mode: endure
            stop-mode: sigterm
            restart-condition: on-success
            install-mode: enable
            aliases:
            - test-alias-1
            - test-alias-2
            environment:
              APP_VARIABLE: test-app-variable
            command-chain:
            - cc-test1
            - cc-test2
            sockets:
              test-socket-1:
                listen-stream: /tmp/test-socket.sock
                socket-mode: 0
              test-socket-2:
                listen-stream: 100
                socket-mode: 1
        confinement: strict
        grade: devel
        environment:
          GLOBAL_VARIABLE: test-global-variable
          LD_LIBRARY_PATH: ${SNAP_LIBRARY_PATH}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
          PATH: $SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH
        plugs:
          empty-plug: null
          string-plug: home
          dict-plug:
            string-parameter: foo
            bool-parameter: true
          content-interface:
            content: test-content
            interface: content
            target: test-target
            default-provider: test-provider
        slots:
          empty-slot: null
          string-slot: slot
          dict-slot:
            string-parameter: foo
            bool-parameter: true
          content-interface:
            interface: content
            read:
            - /
        hooks:
          configure:
            command-chain:
            - test
            environment:
              test-variable-1: test
              test-variable-2: test
            plugs:
            - home
            - network
          install:
            environment:
              environment-var-1: test
        layout:
          /usr/share/libdrm:
            bind: $SNAP/gnome-platform/usr/share/libdrm
          /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0:
            bind: $SNAP/gnome-platform/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0
          /usr/share/xml/iso-codes:
            bind: $SNAP/gnome-platform/usr/share/xml/iso-codes
        system-usernames:
          snap_daemon:
            scope: shared
          snap_microk8s: shared
        """)