Plugins: Setting Env Vars for Stage Test

I’m having an issue where I need to source paths that depend on the stage_dir being the root dir in my plugin test. Wondering how I can access stage_dir from the env or other means while in the plugin tests context, but outside of the actual test code?


I’m not quite clear on what you’re asking. Can you give an example?

@kyrofa, take for example the following

import os
import integration_tests

class RubyPluginTestCase(integration_tests.TestCase):
    def test_ruby_hello(self):
        self.run_snapcraft('stage', 'ruby-hello')
        binary_output = self.get_output_ignoring_non_zero_exit(
            os.path.join(self.stage_dir, 'bin', 'ruby-hello'))
        self.assertEqual("Ruby says, Hello snapcraft.\n", binary_output)
# ruby-hello

set -e

ruby -e "puts 'Ruby says, Hello snapcraft.';"
name: ruby-hello
version: 0.1
summary: Test ruby
description: |
  Snap to test ruby compiled correctly, exists, and env vars are sourced correctly

grade: devmode
confinement: devel

    plugin: ruby
    source: .
      - bin
      - lib
    install: |
      cp ./ruby-hello $SNAPCRAFT_PART_INSTALL/bin/ruby-hello
    plugin: dump
    source: .

My ruby-hello script errors out because it cannot find and successfully execute ruby without knowing RUBYPATH and RUBYLIB. How can I define RUBYPATH and RUBYLIB when they depend on a root of stage_dir, and there is no way for me to access stage_dir from outside of the python plugin test code?

Ah! Yeah trying to run the ruby binary straight out of stage with no environment setup will definitely not work. You need the environment generated by your plugin, but that doesn’t take effect until the prime step, and it’ll still depend on $SNAP being defined, etc. While this test is excellent, I suggest you instead write it as a snap test, in snaps_tests/. That allows you to run the resulting snap directly.

@elopio does that sound okay? I know you’re a little concerned about the size of the snap tests, please throw your opinion in here if you have a better suggestion.

sounds correct. I’m concerned about the snaps tests that don’t have a clear focus on what they are testing, but I think we should install and execute at least one snap for each of our plugins.