# Path: xmanager/contrib/gcs.py
# _GCS_PATH = flags.DEFINE_string(
#     'xm_gcs_path', None, 'A GCS directory within a bucket to store output '
#     '(in gs://bucket/directory format).')
# _GS_PREFIX = 'gs://'
# _GCS_PREFIX = '/gcs/'
# def suggestion(project_name: str) -> str:
# def get_gcs_path_or_fail(project_name: str) -> str:
# def is_gs_path(path: str) -> bool:
# def is_gcs_fuse_path(path: str) -> bool:
# def is_gcs_path(path: str) -> bool:
# def _gcs_path_no_prefix(path: str) -> str:
# def get_gcs_url(path: str) -> str:
# def get_gcs_fuse_path(path: str) -> str:
# def get_gs_path(path: str) -> str:


import getpass
from absl import app
from absl.testing import absltest
from absl.testing import flagsaver
from absl.testing import parameterized
from xmanager.contrib import gcs
_PATH_ERROR = 'Path not in gs://bucket/directory or /gcs/path format'


class GcsTest(parameterized.TestCase):

  def test_gcs_path_empty_flag(self):
    with self.assertRaisesRegex(app.UsageError, '--xm_gcs_path is missing'):
      gcs.get_gcs_path_or_fail('project')

  def test_gcs_path_correct_value(self):
    with flagsaver.flagsaver(xm_gcs_path='gs://bucket/dir'):
      self.assertEqual(gcs.get_gcs_path_or_fail('project'), 'gs://bucket/dir')

  def test_gcs_path_incorrect_value(self):
    with flagsaver.flagsaver(xm_gcs_path='file://dir'):
      with self.assertRaisesRegex(app.UsageError, _GCS_PATH_ERROR):
        gcs.get_gcs_path_or_fail('project')

  # pylint: disable=bad-whitespace
  @parameterized.named_parameters(
      ('gs_long',      'gs://a/b/c', True,  False, True),
      ('gs_short',     'gs://d/e',   True,  False, True),
      ('gs_invalid',   'gs:/d/e',    False, False, False),
      ('fuse_long',    '/gcs/a/b/c', False, True,  True),
      ('fuse_short',   '/gcs/d/e',   False, True,  True),
      ('fuse_invalid', '/gcsc/d/e',  False, False, False),
      ('invalid',      'a/b/f',      False, False, False),
  )  # pyformat:disable
  # pylint: enable=bad-whitespace
  def test_is_path(self, path, expected_gs, expected_fuse, expected_gcs):

# Path: xmanager/contrib/gcs.py
# _GCS_PATH = flags.DEFINE_string(
#     'xm_gcs_path', None, 'A GCS directory within a bucket to store output '
#     '(in gs://bucket/directory format).')
# _GS_PREFIX = 'gs://'
# _GCS_PREFIX = '/gcs/'
# def suggestion(project_name: str) -> str:
# def get_gcs_path_or_fail(project_name: str) -> str:
# def is_gs_path(path: str) -> bool:
# def is_gcs_fuse_path(path: str) -> bool:
# def is_gcs_path(path: str) -> bool:
# def _gcs_path_no_prefix(path: str) -> str:
# def get_gcs_url(path: str) -> str:
# def get_gcs_fuse_path(path: str) -> str:
# def get_gs_path(path: str) -> str:


# Path: xmanager/contrib/gcs_test.py
import getpass
from absl import app
from absl.testing import absltest
from absl.testing import flagsaver
from absl.testing import parameterized
from xmanager.contrib import gcs

_PATH_ERROR = 'Path not in gs://bucket/directory or /gcs/path format'


class GcsTest(parameterized.TestCase):

  def test_gcs_path_empty_flag(self):
    with self.assertRaisesRegex(app.UsageError, '--xm_gcs_path is missing'):
      gcs.get_gcs_path_or_fail('project')

  def test_gcs_path_correct_value(self):
    with flagsaver.flagsaver(xm_gcs_path='gs://bucket/dir'):
      self.assertEqual(gcs.get_gcs_path_or_fail('project'), 'gs://bucket/dir')

  def test_gcs_path_incorrect_value(self):
    with flagsaver.flagsaver(xm_gcs_path='file://dir'):
      with self.assertRaisesRegex(app.UsageError, _GCS_PATH_ERROR):
        gcs.get_gcs_path_or_fail('project')

  # pylint: disable=bad-whitespace
  @parameterized.named_parameters(
      ('gs_long',      'gs://a/b/c', True,  False, True),
      ('gs_short',     'gs://d/e',   True,  False, True),
      ('gs_invalid',   'gs:/d/e',    False, False, False),
      ('fuse_long',    '/gcs/a/b/c', False, True,  True),
      ('fuse_short',   '/gcs/d/e',   False, True,  True),
      ('fuse_invalid', '/gcsc/d/e',  False, False, False),
      ('invalid',      'a/b/f',      False, False, False),
  )  # pyformat:disable
  # pylint: enable=bad-whitespace
  def test_is_path(self, path, expected_gs, expected_fuse, expected_gcs):

    self.assertEqual(gcs.is_gs_path(path), expected_gs)