def test_init_merge_interactive_default(self): tmpdir = mkdtemp(self) stub_stdin(self, u'') with open(os.path.join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0', }}, fd) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--init', '--merge', '--interactive'], name='foo', )) dist.parse_command_line() dist.run_commands() stdout = sys.stdout.getvalue() self.assertIn('+ "jquery": "~1.11.0",', stdout) with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Nothing happened. self.assertEqual(result, { 'dependencies': {'underscore': '~1.8.0'}, 'devDependencies': {'sinon': '~1.17.0'}, })
def test_npm_init_write_name_merge(self): stub_stdouts(self) stub_stdin(self, 'Y') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'jquery': '~1.8.9', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'something_else'}, fd, indent=0) os.chdir(tmpdir) self.assertTrue(npm.npm_init('named', merge=True)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Merge results should be written when user agrees. self.assertEqual(result, { 'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, # name derived from the package_json field. 'name': 'named-js', })
def test_install_no_init_has_package_json_interactive_default_input(self): stub_stdin(self, u'') stub_mod_call(self, cli) tmpdir = mkdtemp(self) with open(os.path.join(tmpdir, 'package.json'), 'w') as fd: json.dump({ 'dependencies': {'jquery': '~3.0.0'}, 'devDependencies': {} }, fd) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['npm', '--install', '--interactive'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Existing package.json will not be overwritten. self.assertEqual(result, { 'dependencies': {'jquery': '~3.0.0'}, 'devDependencies': {}, }) # Ensure that install is NOT called. self.assertIsNone(self.call_args)
def test_npm_init_existing_interactive_merge_no(self): stub_stdouts(self) stub_stdin(self, 'N') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy'}, fd, indent=0) os.chdir(tmpdir) self.assertFalse(npm.npm_init('foo', merge=True, interactive=True)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Should not have written anything if user said no. self.assertEqual(result, { 'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy', })
def test_install_no_init_has_package_json_interactive_default_input(self): stub_stdin(self, u'') stub_mod_call(self, cli) tmpdir = mkdtemp(self) with open(os.path.join(tmpdir, 'package.json'), 'w') as fd: json.dump({ 'dependencies': {'jquery': '~3.0.0'}, 'devDependencies': {} }, fd) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--install', '--interactive'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Existing package.json will not be overwritten. self.assertEqual(result, { 'dependencies': {'jquery': '~3.0.0'}, 'devDependencies': {}, }) # Ensure that install is NOT called. self.assertIsNone(self.call_args)
def test_stub_stdin(self): o_stdin = sys.stdin utils.stub_stdin(self, u'N') self.assertIsNot(o_stdin, sys.stdin) self.assertEqual(sys.stdin.getvalue(), u'N') self.doCleanups() self.assertIs(o_stdin, sys.stdin)
def test_yarn_init_write_name_merge(self): stub_stdouts(self) stub_stdin(self, 'Y') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'jquery': '~1.8.9', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'something_else'}, fd, indent=0) os.chdir(tmpdir) self.assertTrue(yarn.yarn_init('named', merge=True)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Merge results should be written when user agrees. self.assertEqual(result, { 'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, # name derived from the package_json field. 'name': 'named-js', })
def test_yarn_init_existing_interactive_merge_no(self): stub_stdouts(self) stub_stdin(self, 'N') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy'}, fd, indent=0) os.chdir(tmpdir) self.assertFalse(yarn.yarn_init( 'foo', merge=True, callback=prompt_overwrite_json)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Should not have written anything if user said no. self.assertEqual(result, { 'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy', })
def test_init_merge_interactive_default(self): tmpdir = mkdtemp(self) stub_stdin(self, u'') with open(os.path.join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0', }}, fd) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--init', '--merge', '--interactive'], name='foo', )) dist.parse_command_line() dist.run_commands() stdout = sys.stdout.getvalue() self.assertIn('+ "jquery": "~1.11.0",', stdout) with open(os.path.join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Nothing happened. self.assertEqual(result, { 'dependencies': {'underscore': '~1.8.0'}, 'devDependencies': {'sinon': '~1.17.0'}, })
def test_stub_stdin(self): o_stdin = sys.stdin utils.stub_stdin(self, u'N') self.assertIsNot(o_stdin, sys.stdin) self.assertEqual(sys.stdin.getvalue(), u'N') self.doCleanups() self.assertIs(o_stdin, sys.stdin)
def test_yarn_init_existing_merge_interactive_yes(self): stub_stdouts(self) stub_stdin(self, 'Y') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy'}, fd, indent=0) os.chdir(tmpdir) self.assertTrue(yarn.yarn_init('foo', merge=True)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Merge results should be written when user agrees. self.assertEqual(result, { 'dependencies': { 'jquery': '~1.11.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'foo', })
def test_init_no_overwrite_default_input_interactive(self): tmpdir = mkdtemp(self) stub_stdin(self, u'') # default should be no with open(os.path.join(tmpdir, 'package.json'), 'w') as fd: json.dump({ 'dependencies': {}, 'devDependencies': {} }, fd, indent=None) os.chdir(tmpdir) dist = Distribution( dict( script_name='setup.py', script_args=['yarn', '--init', '--interactive'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: # Should not have overwritten result = json.loads(fd.readline()) self.assertEqual(result, { 'dependencies': {}, 'devDependencies': {}, }) stdout = sys.stdout.getvalue() self.assertTrue(stdout.startswith("running yarn\n")) target = join(tmpdir, 'package.json') self.assertIn( "generating a flattened 'package.json' for 'foo'\n" "Generated 'package.json' differs with '%s'" % (target), stdout, ) # That the diff additional block is inside self.assertIn( '+ "dependencies": {\n' '+ "jquery": "~1.11.0"\n' '+ },', stdout, ) self.assertIn( "not overwriting existing '%s'\n" % target, sys.stderr.getvalue(), )
def test_prompt_basic(self): stub_stdouts(self) stub_stdin(self, 'n') result = ui.prompt_overwrite_json( {'a': 1, 'b': 1}, {'a': 1, 'b': 2}, self.tmpjson) self.assertFalse(result) stdout = sys.stdout.getvalue() self.assertIn("'test.json'", stdout) self.assertIn(self.tmpjson, stdout) self.assertIn('- "b": 1', stdout) self.assertIn('+ "b": 2', stdout)
def test_yarn_install_package_json_no_overwrite_interactive(self): """ Most of these package_json testing will be done in the next test class specific for ``yarn init``. """ # Testing the implied init call stub_mod_call(self, cli) stub_stdouts(self) stub_stdin(self, 'n\n') stub_check_interactive(self, True) tmpdir = mkdtemp(self) os.chdir(tmpdir) # All the pre-made setup. app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': { 'jquery': '~1.11.0' }, })), ), 'foo', '1.9.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) stub_item_attr_value(self, dist, 'default_working_set', working_set) # We are going to have a fake package.json with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({}, fd) # capture the logging explicitly as the conditions which # determines how the errors are outputted differs from different # test harnesses. Verify that later. with pretty_logging(stream=StringIO()) as stderr: # This is faked. yarn.yarn_install('foo', callback=prompt_overwrite_json) self.assertIn( "Overwrite '%s'? (Yes/No) [No] " % join(tmpdir, 'package.json'), sys.stdout.getvalue()) # Ensure the error message. Normally this is printed through # stderr via distutils custom logger and our handler bridge for # that which is tested elsewhere. self.assertIn("not continuing with 'yarn install'", stderr.getvalue()) with open(join(tmpdir, 'package.json')) as fd: result = fd.read() # This should remain unchanged as no to overwrite is default. self.assertEqual(result, '{}')
def test_init_no_overwrite_default_input_interactive(self): tmpdir = mkdtemp(self) stub_stdin(self, u'') # default should be no with open(os.path.join(tmpdir, 'package.json'), 'w') as fd: json.dump( {'dependencies': {}, 'devDependencies': {}}, fd, indent=None) os.chdir(tmpdir) dist = Distribution(dict( script_name='setup.py', script_args=['yarn', '--init', '--interactive'], name='foo', )) dist.parse_command_line() dist.run_commands() with open(os.path.join(tmpdir, 'package.json')) as fd: # Should not have overwritten result = json.loads(fd.readline()) self.assertEqual(result, { 'dependencies': {}, 'devDependencies': {}, }) stdout = sys.stdout.getvalue() self.assertTrue(stdout.startswith("running yarn\n")) target = join(tmpdir, 'package.json') self.assertIn( "generating a flattened 'package.json' for 'foo'\n" "Generated 'package.json' differs with '%s'" % (target), stdout, ) # That the diff additional block is inside self.assertIn( '+ "dependencies": {\n' '+ "jquery": "~1.11.0"\n' '+ },', stdout, ) self.assertIn( "not overwriting existing '%s'\n" % target, sys.stderr.getvalue(), )
def test_yarn_init_existing_merge_overwrite(self): stub_stdouts(self) tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump( { 'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy' }, fd, indent=0) os.chdir(tmpdir) # Overwrite will supercede interactive. # stub regardless, when interactive prompt failed to not trigger stub_stdin(self, 'n') self.assertTrue( yarn.yarn_init('foo', merge=True, overwrite=True, callback=prompt_overwrite_json)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Merge results should be written when user agrees. self.assertEqual( result, { 'dependencies': { 'jquery': '~1.11.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'foo', })
def test_yarn_install_package_json_no_overwrite_interactive(self): """ Most of these package_json testing will be done in the next test class specific for ``yarn init``. """ # Testing the implied init call stub_mod_call(self, cli) stub_stdouts(self) stub_stdin(self, 'n\n') stub_check_interactive(self, True) tmpdir = mkdtemp(self) os.chdir(tmpdir) # All the pre-made setup. app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~1.11.0'}, })), ), 'foo', '1.9.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) stub_item_attr_value(self, dist, 'default_working_set', working_set) # We are going to have a fake package.json with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({}, fd) # capture the logging explicitly as the conditions which # determines how the errors are outputted differs from different # test harnesses. Verify that later. with pretty_logging(stream=StringIO()) as stderr: # This is faked. yarn.yarn_install('foo', callback=prompt_overwrite_json) self.assertIn( "Overwrite '%s'? (Yes/No) [No] " % join(tmpdir, 'package.json'), sys.stdout.getvalue()) # Ensure the error message. Normally this is printed through # stderr via distutils custom logger and our handler bridge for # that which is tested elsewhere. self.assertIn("not continuing with 'yarn install'", stderr.getvalue()) with open(join(tmpdir, 'package.json')) as fd: result = fd.read() # This should remain unchanged as no to overwrite is default. self.assertEqual(result, '{}')
def test_yarn_install_package_json_overwrite_interactive(self): # Testing the implied init call stub_mod_call(self, cli) stub_stdin(self, 'y\n') stub_stdouts(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) # All the pre-made setup. app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': { 'jquery': '~1.11.0' }, })), ), 'foo', '1.9.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) stub_item_attr_value(self, dist, 'default_working_set', working_set) # We are going to have a fake package.json with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({}, fd) # This is faked. yarn.yarn_install('foo', overwrite=True) with open(join(tmpdir, 'package.json')) as fd: config = json.load(fd) # Overwritten self.assertEqual( config, { 'dependencies': { 'jquery': '~1.11.0' }, 'devDependencies': {}, 'name': 'foo', }) # No log level set. self.assertEqual(sys.stdout.getvalue(), '') self.assertEqual(sys.stderr.getvalue(), '')
def test_yarn_init_existing_standard_interactive_canceled(self): stub_stdouts(self) stub_stdin(self, 'N') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': {}, 'devDependencies': {}}, fd) os.chdir(tmpdir) self.assertFalse(yarn.yarn_init('foo', callback=prompt_overwrite_json)) with open(join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Does not overwrite by default. self.assertEqual(result, { 'dependencies': {}, 'devDependencies': {}, })
def test_yarn_init_existing_standard_interactive_canceled(self): stub_stdouts(self) stub_stdin(self, 'N') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': {}, 'devDependencies': {}}, fd) os.chdir(tmpdir) self.assertFalse(yarn.yarn_init('foo', callback=prompt_overwrite_json)) with open(join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Does not overwrite by default. self.assertEqual(result, { 'dependencies': {}, 'devDependencies': {}, })
def test_yarn_init_existing_merge_interactive_yes(self): stub_stdouts(self) stub_stdin(self, 'Y') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump( { 'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy' }, fd, indent=0) os.chdir(tmpdir) self.assertTrue(yarn.yarn_init('foo', merge=True)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Merge results should be written when user agrees. self.assertEqual( result, { 'dependencies': { 'jquery': '~1.11.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'foo', })
def test_npm_init_existing_standard_interactive_canceled(self): stub_stdouts(self) stub_stdin(self, 'N') tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': {}, 'devDependencies': {}}, fd) os.chdir(tmpdir) # force autodetected interactivity to be True npm.npm.cli_driver.interactive = True self.assertFalse(npm.npm_init('foo', interactive=True)) with open(join(tmpdir, 'package.json')) as fd: result = json.load(fd) # Does not overwrite by default. self.assertEqual(result, { 'dependencies': {}, 'devDependencies': {}, })
def test_yarn_install_package_json_overwrite_interactive(self): # Testing the implied init call stub_mod_call(self, cli) stub_stdin(self, 'y\n') stub_stdouts(self) tmpdir = mkdtemp(self) os.chdir(tmpdir) # All the pre-made setup. app = make_dummy_dist(self, ( ('requires.txt', '\n'.join([])), ('package.json', json.dumps({ 'dependencies': {'jquery': '~1.11.0'}, })), ), 'foo', '1.9.0') working_set = WorkingSet() working_set.add(app, self._calmjs_testing_tmpdir) stub_item_attr_value(self, dist, 'default_working_set', working_set) # We are going to have a fake package.json with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({}, fd) # This is faked. yarn.yarn_install('foo', overwrite=True) with open(join(tmpdir, 'package.json')) as fd: config = json.load(fd) # Overwritten self.assertEqual(config, { 'dependencies': {'jquery': '~1.11.0'}, 'devDependencies': {}, 'name': 'foo', }) # No log level set. self.assertEqual(sys.stdout.getvalue(), '') self.assertEqual(sys.stderr.getvalue(), '')
def test_yarn_init_existing_merge_overwrite(self): stub_stdouts(self) tmpdir = mkdtemp(self) # Write an initial thing with open(join(tmpdir, 'package.json'), 'w') as fd: json.dump({'dependencies': { 'jquery': '~3.0.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'dummy'}, fd, indent=0) os.chdir(tmpdir) # Overwrite will supercede interactive. # stub regardless, when interactive prompt failed to not trigger stub_stdin(self, 'n') self.assertTrue(yarn.yarn_init( 'foo', merge=True, overwrite=True, callback=prompt_overwrite_json)) with open(join(tmpdir, 'package.json')) as fd: with self.assertRaises(ValueError): json.loads(fd.readline()) fd.seek(0) result = json.load(fd) # Merge results should be written when user agrees. self.assertEqual(result, { 'dependencies': { 'jquery': '~1.11.0', 'underscore': '~1.8.0', }, 'devDependencies': { 'sinon': '~1.17.0' }, 'name': 'foo', })
def test_prompt_true(self): stub_stdouts(self) stub_stdin(self, 'y') result = ui.prompt_overwrite_json( {'a': 1, 'b': 1}, {'a': 1, 'b': 2}, self.tmpjson) self.assertTrue(result)