Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: reserve the --debug option for snapcraft projects #2185

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ snap run:
e.g.; to update the list for 16.04,

./libraries/generate_lib_list.py libraries/16.04

### Enabling debug output

Given that the `--debug` option in snapcraft is reserved for project specific debugging, enabling for the `logger.debug` calls is achieved by setting the "SNAPCRAFT_ENABLE_DEVELOPER_DEBUG" environment variable to a truthful value. Snapcraft's internal tools, e.g.; `snapraftctl` should pick up this environment variable as well.
16 changes: 10 additions & 6 deletions snapcraft/cli/_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging
import os
import sys
from distutils import util

import click

Expand Down Expand Up @@ -55,16 +56,19 @@
)
@click.pass_context
@add_build_options(hidden=True)
@click.option("--debug", "-d", is_flag=True, envvar="SNAPCRAFT_DEBUG")
@click.option("--debug", "-d", is_flag=True)
def run(ctx, debug, catch_exceptions=False, **kwargs):
"""Snapcraft is a delightful packaging tool."""

if debug:
# Debugging snapcraft itself is not tied to debugging a snapcraft project.
try:
is_snapcraft_developer_debug = util.strtobool(
os.getenv("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG", "n")
)
except ValueError:
is_snapcraft_developer_debug = False
if is_snapcraft_developer_debug:
log_level = logging.DEBUG

# Setting this here so that tools run within this are also in debug
# mode (e.g. snapcraftctl)
os.environ["SNAPCRAFT_DEBUG"] = "true"
click.echo(
"Starting snapcraft {} from {}.".format(
snapcraft.__version__, os.path.dirname(__file__)
Expand Down
2 changes: 1 addition & 1 deletion snapcraft/cli/snapcraftctl/_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@click.group()
@click.option("--debug", "-d", is_flag=True, envvar="SNAPCRAFT_DEBUG")
@click.option("--debug", "-d", is_flag=True, envvar="SNAPCRAFT_ENABLE_DEVELOPER_DEBUG")
def run(debug):
"""snapcraftctl is how snapcraft.yaml can communicate with snapcraft"""

Expand Down
6 changes: 2 additions & 4 deletions tests/integration/general/test_list_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@

from snapcraft import plugins

from testtools.matchers import Equals, StartsWith
from testtools.matchers import Equals

from tests import integration


class ListPluginsTestCase(integration.TestCase):
def test_list_plugins(self):
output = self.run_snapcraft("list-plugins")
version, plugins_list = output.split("\n", 1)
self.assertThat(version, StartsWith("Starting snapcraft "))
plugins_list = set(plugins_list.split())
plugins_list = set(output.split())
expected = set()
for _, modname, _ in pkgutil.iter_modules(plugins.__path__):
if not modname.startswith("_"):
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/store/test_store_push_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import subprocess

import fixtures
from testtools.matchers import Contains, FileExists

from tests import integration
Expand Down Expand Up @@ -53,6 +54,10 @@ def test_with_login(self):
self.addCleanup(self.logout)
self.login()

self.useFixture(
fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG", "yes")
)

# Build and register the snap
name, snap_file_path = self._build_snap()
self.register(name)
Expand All @@ -71,6 +76,10 @@ def test_no_push_needed_first(self):
self.addCleanup(self.logout)
self.login()

self.useFixture(
fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG", "yes")
)

# Build and register the snap
name, snap_file_path = self._build_snap()
self.register(name)
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ def setUp(self):
self.base_environment = fixture_setup.FakeBaseEnvironment(machine=machine)
self.useFixture(self.base_environment)

# Make sure SNAPCRAFT_DEBUG is reset between tests
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_DEBUG"))
# Make sure "SNAPCRAFT_ENABLE_DEVELOPER_DEBUG" is reset between tests
self.useFixture(
fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG")
)
self.useFixture(fixture_setup.FakeSnapcraftctl())

def make_snapcraft_yaml(self, content, encoding="utf-8"):
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/commands/test_push_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
from unittest import mock

import fixtures
from testtools.matchers import Contains, Equals, Not

from snapcraft import storeapi
Expand Down Expand Up @@ -93,9 +94,12 @@ def test_simple_debug(self):
self.mock_metadata = patcher.start()
self.addCleanup(patcher.stop)

self.useFixture(
fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG", "yes")
)
# push metadata
with mock.patch("snapcraft.storeapi._status_tracker.StatusTracker"):
result = self.run_command(["--debug", "push-metadata", self.snap_file])
result = self.run_command(["push-metadata", self.snap_file])
self.assertThat(result.exit_code, Equals(0))

self.assertThat(
Expand Down Expand Up @@ -152,10 +156,12 @@ def test_forced(self):
self.mock_metadata = patcher.start()
self.addCleanup(patcher.stop)

result = self.run_command(
["--debug", "push-metadata", self.snap_file, "--force"]
self.useFixture(
fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG", "yes")
)

result = self.run_command(["push-metadata", self.snap_file, "--force"])

self.assertThat(
result.output, Contains("Pushing metadata to the Store (force=True)")
)
Expand Down