import os import sys def test_cli_overrides_sftp_flags(monkeypatch, tmp_path): """Ensure CLI flags override environment for SFTP enabling. We set TRANSFER_FILES False initially then invoke with --enable-sftp and --mock-sftp and a password to verify config adjustments cause the SFTP branch to attempt transfer logic (mocked). Since ENVIRONMENT is TEST, feed generation produces zero files; we focus on env override. """ # Prepare minimal env monkeypatch.setenv('TRANSFER_FILES', 'No') monkeypatch.setenv('ENVIRONMENT', 'TEST') monkeypatch.setenv('FILE_OUTPUT_PATH', str(tmp_path / 'out/{date}')) monkeypatch.setenv('PROCESS_MODE', 'US') # Avoid Snowflake calls by mocking process_table_to_files early. import feed_file_exporter as ffe monkeypatch.setattr(ffe, 'process_table_to_files', lambda *a, **k: []) # Inject a dummy SFTPConfig usage path by mocking transfer function. import sftp_transfer calls = {} def dummy_transfer(files, cfg): # noqa: D401 calls['invoked'] = True calls['files'] = files calls['host'] = cfg.host return None monkeypatch.setattr( sftp_transfer, 'transfer_non_zipped_files_if_enabled', dummy_transfer, ) # Simulate CLI invocation: we reload module with args test_args = [ 'feed_file_exporter.py', '--enable-sftp', '--mock-sftp', '--sftp-password', 'pw123', ] monkeypatch.setenv('PYTEST_RUNNING', '1') monkeypatch.setenv('SFTP_HOST', 'ftp.sme-dsr.com') monkeypatch.setenv('SFTP_USER', 'FTP_Orchard') monkeypatch.setenv('SFTP_FOLDER', 'monthly') # Reload with modified sys.argv monkeypatch.setattr(sys, 'argv', test_args) # Directly invoke CLI parsing helper to apply overrides without # triggering main execution (which would access Snowflake). ffe.parse_dev_cli_args(test_args[1:]) # Trigger SFTP transfer path manually (since main not executed) from sftp_transfer import SFTPConfig, transfer_non_zipped_files_if_enabled cfg = SFTPConfig( host=os.environ.get('SFTP_HOST', 'ftp.sme-dsr.com'), username=os.environ.get('SFTP_USER', 'FTP_Orchard'), password=os.environ.get('SFTP_PASSWORD'), remote_dir=os.environ.get('SFTP_FOLDER', 'monthly'), ) transfer_non_zipped_files_if_enabled(['dummy.txt'], cfg) # Assertions assert os.environ.get('SFTP_PASSWORD') == 'pw123' assert os.environ.get('TRANSFER_FILES') == 'True' assert os.environ.get('SFTP_MOCK') == 'True' assert calls.get('invoked') is True