Repository: charmed-kubernetes/pytest-operator (language: python)

Issue:
Building charm with multiple bases not handled correctly
When we run `ops_test.build_charm()` a charm with multiple bases, the returned charm path is arbitrarily picked and there is no ability to select the `series` of the built charm.

I believe that this arbitrary selection of charm file is happening [here](https://github.com/charmed-kubernetes/pytest-operator/blob/main/pytest_operator/plugin.py#L981)


Discussion / hints:

Fix patch:
diff --git a/docs/reference.md b/docs/reference.md
index ad3cb31..93d8116 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -154,7 +154,7 @@ Dataclass which represents a juju bundle.
 
 ### Methods
 
-#### `async def build_charm(self, charm_path)`
+#### `async def build_charm(self, charm_path, bases_index = None)`
 
 Builds a charm.
 


Test patch:
diff --git a/pytest_operator/plugin.py b/pytest_operator/plugin.py
index be8a7ae..850c309 100644
--- a/pytest_operator/plugin.py
+++ b/pytest_operator/plugin.py
@@ -907,7 +907,7 @@ def abort(self, *args, **kwargs):
         self.aborted = True
         pytest.fail(*args, **kwargs)
 
-    async def build_charm(self, charm_path) -> Path:
+    async def build_charm(self, charm_path, bases_index: int = None) -> Path:
         """Builds a single charm.
 
         This can handle charms using the older charms.reactive framework as
@@ -930,20 +930,20 @@ async def build_charm(self, charm_path) -> Path:
             # Handle newer, operator framework charms.
             all_groups = {g.gr_name for g in grp.getgrall()}
             users_groups = {grp.getgrgid(g).gr_name for g in os.getgroups()}
+            cmd = ["charmcraft", "pack"]
+            if bases_index:
+                cmd.append(f"--bases-index={bases_index}")
             if self.destructive_mode:
                 # host builder never requires lxd group
-                cmd = ["charmcraft", "pack", "--destructive-mode"]
-            elif "lxd" in users_groups:
-                # user already has lxd group active
-                cmd = ["charmcraft", "pack"]
-            else:
+                cmd.append("--destructive-mode")
+            elif "lxd" not in users_groups:
                 # building with lxd builder and user does't already have lxd group;
                 # make sure it's available and if so, try using `sg` to acquire it
                 assert "lxd" in all_groups, (
                     "Group 'lxd' required but not available; "
                     "ensure that lxd is available or use --destructive-mode"
                 )
-                cmd = ["sg", "lxd", "-c", "charmcraft pack"]
+                cmd = ["sg", "lxd", "-c", " ".join(cmd)]
 
         log.info(f"Building charm {charm_name}")
         start = timer()